123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- //
- // NIMAvatarImageView.m
- // NIMKit
- //
- // Created by chris on 15/2/10.
- // Copyright (c) 2015年 Netease. All rights reserved.
- //
- #import "NIMAvatarImageView.h"
- #import "UIView+NIM.h"
- #import "objc/runtime.h"
- #import "NIMKitDependency.h"
- #import "NIMKit.h"
- #import "NIMKitInfoFetchOption.h"
- static char imageURLKey;
- @interface NIMAvatarImageView()
- @property (nonatomic,strong) UIImageView *imageView;
- @end
- @implementation NIMAvatarImageView
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self)
- {
- [self setup];
- }
- return self;
- }
- - (id)initWithCoder:(NSCoder *)aDecoder
- {
- if (self = [super initWithCoder:aDecoder])
- {
- [self setup];
- }
- return self;
- }
- - (void)setup
- {
- _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
- _imageView.contentMode = UIViewContentModeScaleAspectFill;
- [self addSubview:_imageView];
- self.backgroundColor = [UIColor clearColor];
- [self setupRadius];
- }
- - (void)setupRadius
- {
- switch ([NIMKit sharedKit].config.avatarType)
- {
- case NIMKitAvatarTypeNone:
- _cornerRadius = 0;
- break;
- case NIMKitAvatarTypeRounded:
- _cornerRadius = self.nim_width *.5f;
- break;
- case NIMKitAvatarTypeRadiusCorner:
- _cornerRadius = 10.f;
- break;
- default:
- break;
- }
- }
- //会话列表头像
- - (void)setImage:(UIImage *)image
- {
- if (_image != image)
- {
- _image = image;
- self.imageView.image = image;
- self.imageView.clipsToBounds = YES;
- self.imageView.layer.cornerRadius = _cornerRadius;
- // UIImage *fixedImage = [self imageAddCornerWithRadius:_cornerRadius andSize:self.bounds.size];
- // self.imageView.image = fixedImage;
- }
- }
- - (UIImage*)imageAddCornerWithRadius:(CGFloat)radius andSize:(CGSize)size
- {
- CGRect rect = CGRectMake(0, 0, size.width, size.height);
- // 传入的View.frame.size是0的话,直接返回nil,防止 UIGraphicsBeginImageContext() 传入0,导致崩溃
- if (CGSizeEqualToSize(size, CGSizeZero)) {
- return nil;
- }
- UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
- CGContextRef ctx = UIGraphicsGetCurrentContext();
- CGPathRef path = self.path;
- CGContextAddPath(ctx,path);
- CGContextClip(ctx);
- [self.image drawInRect:rect];
- CGContextDrawPath(ctx, kCGPathFillStroke);
- UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return newImage;
- }
- - (CGPathRef)path
- {
- return [[UIBezierPath bezierPathWithRoundedRect:self.bounds
- cornerRadius:self.cornerRadius] CGPath];
- }
- - (void)setAvatarBySession:(NIMSession *)session
- {
- NIMKitInfo *info = nil;
- if (session.sessionType == NIMSessionTypeTeam)
- {
- info = [[NIMKit sharedKit] infoByTeam:session.sessionId option:nil];
- }
- else
- {
- NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
- option.session = session;
- info = [[NIMKit sharedKit] infoByUser:session.sessionId option:option];
- }
- NSURL *url = info.avatarUrlString ? [NSURL URLWithString:info.avatarUrlString] : nil;
- NIMUser* nimUser = [[[NIMSDK sharedSDK] userManager] userInfo:session.sessionId];
- if(nimUser.userInfo.gender == NIMUserGenderMale){//默认男头像
- info.avatarImage = [UIImage imageNamed:@"woman"];
- }else{
- info.avatarImage = [UIImage imageNamed:@"vqu_images_P_man"];
- }
- [self nim_setImageWithURL:[[LCTools getImageUrlWithAddress:[url absoluteString]] urlWithImageScale:60] placeholderImage:info.avatarImage];
- }
- - (void)setAvatarByMessage:(NIMMessage *)message
- {
- NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
- option.message = message;
- NSString *from = nil;
- if (message.messageType == NIMMessageTypeRobot)
- {
- NIMRobotObject *object = (NIMRobotObject *)message.messageObject;
- if (object.isFromRobot)
- {
- from = object.robotId;
- }
- }
- if (!from)
- {
- from = message.from;
- }
- NIMKitInfo *info = [[NIMKit sharedKit] infoByUser:from option:option];
- NSURL *url = info.avatarUrlString ? [NSURL URLWithString:info.avatarUrlString] : nil;
- NIMUser* nimUser = [[[NIMSDK sharedSDK] userManager] userInfo:from];
- if(nimUser.userInfo.gender == NIMUserGenderMale){//网易云的男女头像和本系统的头像相反
- info.avatarImage = [UIImage imageNamed:@"woman"];
- }else{
- info.avatarImage = [UIImage imageNamed:@"vqu_images_P_man"];
- }
- [self nim_setImageWithURL:[[LCTools getImageUrlWithAddress:[url absoluteString]] urlWithImageScale:60] placeholderImage:info.avatarImage];
- }
- @end
- @implementation NIMAvatarImageView (SDWebImageCache)
- - (void)nim_setImageWithURL:(NSURL *)url {
- [self nim_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
- }
- - (void)nim_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder {
- [self nim_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil];
- }
- - (void)nim_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options {
- [self nim_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil];
- }
- - (void)nim_setImageWithURL:(NSURL *)url completed:(SDExternalCompletionBlock)completedBlock {
- [self nim_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:completedBlock];
- }
- - (void)nim_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDExternalCompletionBlock)completedBlock {
- [self nim_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:completedBlock];
- }
- - (void)nim_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDExternalCompletionBlock)completedBlock {
- [self nim_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
- }
- - (void)nim_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDExternalCompletionBlock)completedBlock {
- NSString *validOperationKey = NSStringFromClass([self class]);
- [self sd_cancelImageLoadOperationWithKey:validOperationKey];
- objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
-
- if (!(options & SDWebImageDelayPlaceholder)) {
- dispatch_main_async_safe(^{
- [self nim_setImage:placeholder imageData:nil basedOnClassOrViaCustomSetImageBlock:nil];
- });
- }
-
- if (url) {
- // check if activityView is enabled or not
- if ([self sd_showActivityIndicatorView]) {
- [self sd_addActivityIndicator];
- }
-
- __weak __typeof(self)wself = self;
- id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager loadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
- __strong __typeof (wself) sself = wself;
- [sself sd_removeActivityIndicator];
- if (!sself) {
- return;
- }
- dispatch_main_async_safe(^{
- if (!sself) {
- return;
- }
- if (image && (options & SDWebImageAvoidAutoSetImage) && completedBlock) {
- completedBlock(image, error, cacheType, url);
- return;
- } else if (image) {
- [sself nim_setImage:image imageData:data basedOnClassOrViaCustomSetImageBlock:nil];
- [sself nim_setNeedsLayout];
- } else {
- if ((options & SDWebImageDelayPlaceholder)) {
- [sself nim_setImage:placeholder imageData:nil basedOnClassOrViaCustomSetImageBlock:nil];
- [sself nim_setNeedsLayout];
- }
- }
- if (completedBlock && finished) {
- completedBlock(image, error, cacheType, url);
- }
- });
- }];
- [self sd_setImageLoadOperation:operation forKey:validOperationKey];
- } else {
- dispatch_main_async_safe(^{
- [self sd_removeActivityIndicator];
- if (completedBlock) {
- NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
- completedBlock(nil, error, SDImageCacheTypeNone, url);
- }
- });
- }
- }
- - (void)nim_setImageWithPreviousCachedImageWithURL:(NSURL *)url andPlaceholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDExternalCompletionBlock)completedBlock {
- NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:url];
- UIImage *lastPreviousCachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key];
-
- [self nim_setImageWithURL:url placeholderImage:lastPreviousCachedImage ?: placeholder options:options progress:progressBlock completed:completedBlock];
- }
- - (void)nim_setImage:(UIImage *)image imageData:(NSData *)imageData basedOnClassOrViaCustomSetImageBlock:(SDSetImageBlock)setImageBlock {
- if (setImageBlock) {
- setImageBlock(image, imageData);
- return;
- }
- self.image = image;
- }
- - (void)nim_setNeedsLayout {
- [self setNeedsLayout];
- }
- - (NSURL *)nim_imageURL {
- return objc_getAssociatedObject(self, &imageURLKey);
- }
- - (void)nim_cancelCurrentImageLoad {
- [self sd_cancelImageLoadOperationWithKey:@"UIImageViewImageLoad"];
- }
- - (void)nim_cancelCurrentAnimationImagesLoad {
- [self sd_cancelImageLoadOperationWithKey:@"UIImageViewAnimationImages"];
- }
- @end
|