NIMAvatarImageView.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // NIMAvatarImageView.m
  3. // NIMKit
  4. //
  5. // Created by chris on 15/2/10.
  6. // Copyright (c) 2015年 Netease. All rights reserved.
  7. //
  8. #import "NIMAvatarImageView.h"
  9. #import "UIView+NIM.h"
  10. #import "objc/runtime.h"
  11. #import "NIMKitDependency.h"
  12. #import "NIMKit.h"
  13. #import "NIMKitInfoFetchOption.h"
  14. static char imageURLKey;
  15. @interface NIMAvatarImageView()
  16. @property (nonatomic,strong) UIImageView *imageView;
  17. @end
  18. @implementation NIMAvatarImageView
  19. - (id)initWithFrame:(CGRect)frame
  20. {
  21. self = [super initWithFrame:frame];
  22. if (self)
  23. {
  24. [self setup];
  25. }
  26. return self;
  27. }
  28. - (id)initWithCoder:(NSCoder *)aDecoder
  29. {
  30. if (self = [super initWithCoder:aDecoder])
  31. {
  32. [self setup];
  33. }
  34. return self;
  35. }
  36. - (void)setup
  37. {
  38. _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
  39. _imageView.contentMode = UIViewContentModeScaleAspectFill;
  40. [self addSubview:_imageView];
  41. self.backgroundColor = [UIColor clearColor];
  42. [self setupRadius];
  43. }
  44. - (void)setupRadius
  45. {
  46. switch ([NIMKit sharedKit].config.avatarType)
  47. {
  48. case NIMKitAvatarTypeNone:
  49. _cornerRadius = 0;
  50. break;
  51. case NIMKitAvatarTypeRounded:
  52. _cornerRadius = self.nim_width *.5f;
  53. break;
  54. case NIMKitAvatarTypeRadiusCorner:
  55. _cornerRadius = 10.f;
  56. break;
  57. default:
  58. break;
  59. }
  60. }
  61. //会话列表头像
  62. - (void)setImage:(UIImage *)image
  63. {
  64. if (_image != image)
  65. {
  66. _image = image;
  67. self.imageView.image = image;
  68. self.imageView.clipsToBounds = YES;
  69. self.imageView.layer.cornerRadius = _cornerRadius;
  70. // UIImage *fixedImage = [self imageAddCornerWithRadius:_cornerRadius andSize:self.bounds.size];
  71. // self.imageView.image = fixedImage;
  72. }
  73. }
  74. - (UIImage*)imageAddCornerWithRadius:(CGFloat)radius andSize:(CGSize)size
  75. {
  76. CGRect rect = CGRectMake(0, 0, size.width, size.height);
  77. // 传入的View.frame.size是0的话,直接返回nil,防止 UIGraphicsBeginImageContext() 传入0,导致崩溃
  78. if (CGSizeEqualToSize(size, CGSizeZero)) {
  79. return nil;
  80. }
  81. UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
  82. CGContextRef ctx = UIGraphicsGetCurrentContext();
  83. CGPathRef path = self.path;
  84. CGContextAddPath(ctx,path);
  85. CGContextClip(ctx);
  86. [self.image drawInRect:rect];
  87. CGContextDrawPath(ctx, kCGPathFillStroke);
  88. UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
  89. UIGraphicsEndImageContext();
  90. return newImage;
  91. }
  92. - (CGPathRef)path
  93. {
  94. return [[UIBezierPath bezierPathWithRoundedRect:self.bounds
  95. cornerRadius:self.cornerRadius] CGPath];
  96. }
  97. - (void)setAvatarBySession:(NIMSession *)session
  98. {
  99. NIMKitInfo *info = nil;
  100. if (session.sessionType == NIMSessionTypeTeam)
  101. {
  102. info = [[NIMKit sharedKit] infoByTeam:session.sessionId option:nil];
  103. }
  104. else
  105. {
  106. NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
  107. option.session = session;
  108. info = [[NIMKit sharedKit] infoByUser:session.sessionId option:option];
  109. }
  110. NSURL *url = info.avatarUrlString ? [NSURL URLWithString:info.avatarUrlString] : nil;
  111. NIMUser* nimUser = [[[NIMSDK sharedSDK] userManager] userInfo:session.sessionId];
  112. if(nimUser.userInfo.gender == NIMUserGenderMale){//默认男头像
  113. info.avatarImage = [UIImage imageNamed:@"woman"];
  114. }else{
  115. info.avatarImage = [UIImage imageNamed:@"vqu_images_P_man"];
  116. }
  117. [self nim_setImageWithURL:[[LCTools getImageUrlWithAddress:[url absoluteString]] urlWithImageScale:60] placeholderImage:info.avatarImage];
  118. }
  119. - (void)setAvatarByMessage:(NIMMessage *)message
  120. {
  121. NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
  122. option.message = message;
  123. NSString *from = nil;
  124. if (message.messageType == NIMMessageTypeRobot)
  125. {
  126. NIMRobotObject *object = (NIMRobotObject *)message.messageObject;
  127. if (object.isFromRobot)
  128. {
  129. from = object.robotId;
  130. }
  131. }
  132. if (!from)
  133. {
  134. from = message.from;
  135. }
  136. NIMKitInfo *info = [[NIMKit sharedKit] infoByUser:from option:option];
  137. NSURL *url = info.avatarUrlString ? [NSURL URLWithString:info.avatarUrlString] : nil;
  138. NIMUser* nimUser = [[[NIMSDK sharedSDK] userManager] userInfo:from];
  139. if(nimUser.userInfo.gender == NIMUserGenderMale){//网易云的男女头像和本系统的头像相反
  140. info.avatarImage = [UIImage imageNamed:@"woman"];
  141. }else{
  142. info.avatarImage = [UIImage imageNamed:@"vqu_images_P_man"];
  143. }
  144. [self nim_setImageWithURL:[[LCTools getImageUrlWithAddress:[url absoluteString]] urlWithImageScale:60] placeholderImage:info.avatarImage];
  145. }
  146. @end
  147. @implementation NIMAvatarImageView (SDWebImageCache)
  148. - (void)nim_setImageWithURL:(NSURL *)url {
  149. [self nim_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
  150. }
  151. - (void)nim_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder {
  152. [self nim_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil];
  153. }
  154. - (void)nim_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options {
  155. [self nim_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil];
  156. }
  157. - (void)nim_setImageWithURL:(NSURL *)url completed:(SDExternalCompletionBlock)completedBlock {
  158. [self nim_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:completedBlock];
  159. }
  160. - (void)nim_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDExternalCompletionBlock)completedBlock {
  161. [self nim_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:completedBlock];
  162. }
  163. - (void)nim_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDExternalCompletionBlock)completedBlock {
  164. [self nim_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
  165. }
  166. - (void)nim_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDExternalCompletionBlock)completedBlock {
  167. NSString *validOperationKey = NSStringFromClass([self class]);
  168. [self sd_cancelImageLoadOperationWithKey:validOperationKey];
  169. objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  170. if (!(options & SDWebImageDelayPlaceholder)) {
  171. dispatch_main_async_safe(^{
  172. [self nim_setImage:placeholder imageData:nil basedOnClassOrViaCustomSetImageBlock:nil];
  173. });
  174. }
  175. if (url) {
  176. // check if activityView is enabled or not
  177. if ([self sd_showActivityIndicatorView]) {
  178. [self sd_addActivityIndicator];
  179. }
  180. __weak __typeof(self)wself = self;
  181. id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager loadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
  182. __strong __typeof (wself) sself = wself;
  183. [sself sd_removeActivityIndicator];
  184. if (!sself) {
  185. return;
  186. }
  187. dispatch_main_async_safe(^{
  188. if (!sself) {
  189. return;
  190. }
  191. if (image && (options & SDWebImageAvoidAutoSetImage) && completedBlock) {
  192. completedBlock(image, error, cacheType, url);
  193. return;
  194. } else if (image) {
  195. [sself nim_setImage:image imageData:data basedOnClassOrViaCustomSetImageBlock:nil];
  196. [sself nim_setNeedsLayout];
  197. } else {
  198. if ((options & SDWebImageDelayPlaceholder)) {
  199. [sself nim_setImage:placeholder imageData:nil basedOnClassOrViaCustomSetImageBlock:nil];
  200. [sself nim_setNeedsLayout];
  201. }
  202. }
  203. if (completedBlock && finished) {
  204. completedBlock(image, error, cacheType, url);
  205. }
  206. });
  207. }];
  208. [self sd_setImageLoadOperation:operation forKey:validOperationKey];
  209. } else {
  210. dispatch_main_async_safe(^{
  211. [self sd_removeActivityIndicator];
  212. if (completedBlock) {
  213. NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
  214. completedBlock(nil, error, SDImageCacheTypeNone, url);
  215. }
  216. });
  217. }
  218. }
  219. - (void)nim_setImageWithPreviousCachedImageWithURL:(NSURL *)url andPlaceholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDExternalCompletionBlock)completedBlock {
  220. NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:url];
  221. UIImage *lastPreviousCachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key];
  222. [self nim_setImageWithURL:url placeholderImage:lastPreviousCachedImage ?: placeholder options:options progress:progressBlock completed:completedBlock];
  223. }
  224. - (void)nim_setImage:(UIImage *)image imageData:(NSData *)imageData basedOnClassOrViaCustomSetImageBlock:(SDSetImageBlock)setImageBlock {
  225. if (setImageBlock) {
  226. setImageBlock(image, imageData);
  227. return;
  228. }
  229. self.image = image;
  230. }
  231. - (void)nim_setNeedsLayout {
  232. [self setNeedsLayout];
  233. }
  234. - (NSURL *)nim_imageURL {
  235. return objc_getAssociatedObject(self, &imageURLKey);
  236. }
  237. - (void)nim_cancelCurrentImageLoad {
  238. [self sd_cancelImageLoadOperationWithKey:@"UIImageViewImageLoad"];
  239. }
  240. - (void)nim_cancelCurrentAnimationImagesLoad {
  241. [self sd_cancelImageLoadOperationWithKey:@"UIImageViewAnimationImages"];
  242. }
  243. @end