123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- //
- // UIImage+NIMKit.m
- // NIMKit
- //
- // Created by chris.
- // Copyright (c) 2015年 NetEase. All rights reserved.
- //
- #import "UIImage+NIMKit.h"
- #import "NIMInputEmoticonDefine.h"
- #import "NIMKit.h"
- #import "NIMKitDevice.h"
- @implementation UIImage (NIMKit)
- + (UIImage *)nim_fetchEmoticon:(NSString *)imageNameOrPath{
- UIImage *image = [UIImage nim_emoticonInKit:imageNameOrPath];
- if (!image) {
- image = [UIImage imageWithContentsOfFile:imageNameOrPath];
- }
- return image;
- }
- + (UIImage *)nim_fetchChartlet:(NSString *)imageName chartletId:(NSString *)chartletId{
- if ([chartletId isEqualToString:NIMKit_EmojiCatalog]) {
- return [UIImage imageNamed:imageName];
- }
- NSString *subDirectory = [NSString stringWithFormat:@"%@/%@/%@",NIMKit_ChartletChartletCatalogPath,chartletId,NIMKit_ChartletChartletCatalogContentPath];
- //先拿2倍图
- NSString *doubleImage = [imageName stringByAppendingString:@"@2x"];
- NSString *tribleImage = [imageName stringByAppendingString:@"@3x"];
- NSString *bundlePath = [[NSBundle mainBundle].bundlePath stringByAppendingPathComponent:subDirectory];
- NSString *path = nil;
-
- NSArray *array = [NSBundle pathsForResourcesOfType:nil inDirectory:bundlePath];
- NSString *fileExt = [[array.firstObject lastPathComponent] pathExtension];
- if ([UIScreen mainScreen].scale == 3.0) {
- path = [NSBundle pathForResource:tribleImage ofType:fileExt inDirectory:bundlePath];
- }
- path = path ? path : [NSBundle pathForResource:doubleImage ofType:fileExt inDirectory:bundlePath]; //取二倍图
- path = path ? path : [NSBundle pathForResource:imageName ofType:fileExt inDirectory:bundlePath]; //实在没了就去取一倍图
- return [UIImage imageWithContentsOfFile:path];
- }
- + (CGSize)nim_sizeWithImageOriginSize:(CGSize)originSize
- minSize:(CGSize)imageMinSize
- maxSize:(CGSize)imageMaxSiz{
- CGSize size;
- NSInteger imageWidth = originSize.width ,imageHeight = originSize.height;
- NSInteger imageMinWidth = imageMinSize.width, imageMinHeight = imageMinSize.height;
- NSInteger imageMaxWidth = imageMaxSiz.width, imageMaxHeight = imageMaxSiz.height;
- if (imageWidth > imageHeight) //宽图
- {
- size.height = imageMinHeight; //高度取最小高度
- size.width = imageWidth * imageMinHeight / imageHeight;
- if (size.width > imageMaxWidth)
- {
- size.width = imageMaxWidth;
- }
- }
- else if(imageWidth < imageHeight)//高图
- {
- size.width = imageMinWidth;
- size.height = imageHeight *imageMinWidth / imageWidth;
- if (size.height > imageMaxHeight)
- {
- size.height = imageMaxHeight;
- }
- }
- else//方图
- {
- if (imageWidth > imageMaxWidth)
- {
- size.width = imageMaxWidth;
- size.height = imageMaxHeight;
- }
- else if(imageWidth > imageMinWidth)
- {
- size.width = imageWidth;
- size.height = imageHeight;
- }
- else
- {
- size.width = imageMinWidth;
- size.height = imageMinHeight;
- }
- }
- return size;
- }
- + (UIImage *)nim_imageInKit:(NSString *)imageName{
- NSString *name = [[[NIMKit sharedKit] resourceBundleName] stringByAppendingPathComponent:imageName];
- UIImage *image = [UIImage imageNamed:imageName];
- //优先取上层bundle 里的图片,如果没有,则用自带资源的图片
- return image? image : [UIImage imageNamed:name];
- }
- + (UIImage *)nim_emoticonInKit:(NSString *)imageName
- {
- NSString *name = [[[NIMKit sharedKit] emoticonBundleName] stringByAppendingPathComponent:imageName];
- UIImage *image = [UIImage imageNamed:name];
- return image;
- }
- - (UIImage *)nim_imageForAvatarUpload
- {
- CGFloat pixels = [[NIMKitDevice currentDevice] suggestImagePixels];
- UIImage * image = [self nim_imageForUpload:pixels];
- return [image nim_fixOrientation];
- }
- #pragma mark - Private
- - (UIImage *)nim_imageForUpload: (CGFloat)suggestPixels
- {
- CGFloat maxPixels = 4000000;
- CGFloat maxRatio = 3;
-
- CGFloat width = self.size.width;
- CGFloat height= self.size.height;
-
- //对于超过建议像素,且长宽比超过max ratio的图做特殊处理
- if (width * height > suggestPixels &&
- (width / height > maxRatio || height / width > maxRatio))
- {
- return [self nim_scaleWithMaxPixels:maxPixels];
- }
- else
- {
- return [self nim_scaleWithMaxPixels:suggestPixels];
- }
- }
- - (UIImage *)nim_scaleWithMaxPixels: (CGFloat)maxPixels
- {
- CGFloat width = self.size.width;
- CGFloat height= self.size.height;
- if (width * height < maxPixels || maxPixels == 0)
- {
- return self;
- }
- CGFloat ratio = sqrt(width * height / maxPixels);
- if (fabs(ratio - 1) <= 0.01)
- {
- return self;
- }
- CGFloat newSizeWidth = width / ratio;
- CGFloat newSizeHeight= height/ ratio;
- return [self nim_scaleToSize:CGSizeMake(newSizeWidth, newSizeHeight)];
- }
- //内缩放,一条变等于最长边,另外一条小于等于最长边
- - (UIImage *)nim_scaleToSize:(CGSize)newSize
- {
- CGFloat width = self.size.width;
- CGFloat height= self.size.height;
- CGFloat newSizeWidth = newSize.width;
- CGFloat newSizeHeight= newSize.height;
- if (width <= newSizeWidth &&
- height <= newSizeHeight)
- {
- return self;
- }
-
- if (width == 0 || height == 0 || newSizeHeight == 0 || newSizeWidth == 0)
- {
- return nil;
- }
- CGSize size;
- if (width / height > newSizeWidth / newSizeHeight)
- {
- size = CGSizeMake(newSizeWidth, newSizeWidth * height / width);
- }
- else
- {
- size = CGSizeMake(newSizeHeight * width / height, newSizeHeight);
- }
- return [self nim_drawImageWithSize:size];
- }
- - (UIImage *)nim_drawImageWithSize: (CGSize)size
- {
- CGSize drawSize = CGSizeMake(floor(size.width), floor(size.height));
- // 传入的View.frame.size是0的话,直接返回nil,防止 UIGraphicsBeginImageContext() 传入0,导致崩溃
- if (CGSizeEqualToSize(size, CGSizeZero)) {
- return nil;
- }
- UIGraphicsBeginImageContext(drawSize);
-
- [self drawInRect:CGRectMake(0, 0, drawSize.width, drawSize.height)];
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return newImage;
- }
- - (UIImage *)nim_fixOrientation
- {
-
- // No-op if the orientation is already correct
- if (self.imageOrientation == UIImageOrientationUp)
- return self;
-
- // We need to calculate the proper transformation to make the image upright.
- // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
- CGAffineTransform transform = CGAffineTransformIdentity;
-
- switch (self.imageOrientation) {
- case UIImageOrientationDown:
- case UIImageOrientationDownMirrored:
- transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
- transform = CGAffineTransformRotate(transform, M_PI);
- break;
-
- case UIImageOrientationLeft:
- case UIImageOrientationLeftMirrored:
- transform = CGAffineTransformTranslate(transform, self.size.width, 0);
- transform = CGAffineTransformRotate(transform, M_PI_2);
- break;
-
- case UIImageOrientationRight:
- case UIImageOrientationRightMirrored:
- transform = CGAffineTransformTranslate(transform, 0, self.size.height);
- transform = CGAffineTransformRotate(transform, -M_PI_2);
- break;
- default:
- break;
- }
-
- switch (self.imageOrientation) {
- case UIImageOrientationUpMirrored:
- case UIImageOrientationDownMirrored:
- transform = CGAffineTransformTranslate(transform, self.size.width, 0);
- transform = CGAffineTransformScale(transform, -1, 1);
- break;
-
- case UIImageOrientationLeftMirrored:
- case UIImageOrientationRightMirrored:
- transform = CGAffineTransformTranslate(transform, self.size.height, 0);
- transform = CGAffineTransformScale(transform, -1, 1);
- break;
- default:
- break;
- }
-
- // Now we draw the underlying CGImage into a new context, applying the transform
- // calculated above.
- CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
- CGImageGetBitsPerComponent(self.CGImage), 0,
- CGImageGetColorSpace(self.CGImage),
- CGImageGetBitmapInfo(self.CGImage));
- CGContextConcatCTM(ctx, transform);
- switch (self.imageOrientation) {
- case UIImageOrientationLeft:
- case UIImageOrientationLeftMirrored:
- case UIImageOrientationRight:
- case UIImageOrientationRightMirrored:
- // Grr...
- CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
- break;
-
- default:
- CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
- break;
- }
-
- // And now we just create a new UIImage from the drawing context
- CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
- UIImage *img = [UIImage imageWithCGImage:cgimg];
- CGContextRelease(ctx);
- CGImageRelease(cgimg);
- return img;
- }
- @end
|