// // UIImage+Utils.m // YYMobileFramework // // Created by wuwei on 14/6/20. // Copyright (c) 2014年 YY Inc. All rights reserved. // #import "UIImage+Utils.h" #import @implementation UIImage (Utils) - (UIImage *)grayscaleImage { CGFloat width = self.size.width; CGFloat height = self.size.height; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); CGContextRef context = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpace, kCGImageAlphaNone); // kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast); if (context == NULL) { return nil; } CGContextDrawImage(context, CGRectMake(0, 0, width, height), self.CGImage); CGImageRef imageRef = CGBitmapContextCreateImage(context); UIImage *grayscaleImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); CGContextRelease(context); CGColorSpaceRelease(colorSpace); return grayscaleImage; } - (UIImage *)imageBlendInGray { UIGraphicsBeginImageContext(self.size); CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextFillRect(context, bounds); [self drawInRect:bounds blendMode:kCGBlendModeLuminosity alpha:1.0f]; [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f]; UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImg; } - (UIImage *)imageWithBlendMode:(CGBlendMode)blendMode { UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f); CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height); UIRectFill(bounds); [self drawInRect:bounds blendMode:blendMode alpha:1.0f]; if (blendMode != kCGBlendModeDestinationIn) { [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f]; } UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImg; } + (UIImage *)imageWithColor:(UIColor *)color { return [self imageWithColor:color size:CGSizeMake(1, 1)]; } + (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size { if (!color || size.width <= 0 || size.height <= 0) return nil; CGRect rect = CGRectMake(0.0f, 0.0f, size.width + 1, size.height); UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, color.CGColor); CGContextFillRect(context, rect); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } + (UIImage *)fixOrientation:(UIImage *)aImage { // No-op if the orientation is already correct if (aImage.imageOrientation == UIImageOrientationUp) return aImage; // 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 (aImage.imageOrientation) { case UIImageOrientationDown: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height); transform = CGAffineTransformRotate(transform, M_PI); break; case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: transform = CGAffineTransformTranslate(transform, aImage.size.width, 0); transform = CGAffineTransformRotate(transform, M_PI_2); break; case UIImageOrientationRight: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, 0, aImage.size.height); transform = CGAffineTransformRotate(transform, -M_PI_2); break; default: break; } switch (aImage.imageOrientation) { case UIImageOrientationUpMirrored: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, aImage.size.width, 0); transform = CGAffineTransformScale(transform, -1, 1); break; case UIImageOrientationLeftMirrored: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, aImage.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, aImage.size.width, aImage.size.height, CGImageGetBitsPerComponent(aImage.CGImage), 0, CGImageGetColorSpace(aImage.CGImage), CGImageGetBitmapInfo(aImage.CGImage)); CGContextConcatCTM(ctx, transform); switch (aImage.imageOrientation) { case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: case UIImageOrientationRight: case UIImageOrientationRightMirrored: // Grr... CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage); break; default: CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.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; } - (UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, rect); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } //异步生成纯色圆角图片 - (void)imageWithSize:(CGSize)size radius:(CGFloat)radius backColor:(UIColor *)backColor completion:(void(^)(UIImage *image))completion { // 异步绘制裁切 dispatch_async(dispatch_get_global_queue(0, 0), ^{ // 利用绘图建立上下文 UIGraphicsBeginImageContextWithOptions(size, true, 0); CGRect rect = CGRectMake(0, 0, size.width, size.height); // 填充颜色 [backColor setFill]; UIRectFill(rect); // // 贝塞尔裁切 // UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius]; // [path addClip]; // [self drawInRect:rect]; // 获取结果 UIImage *resultImage = [UIGraphicsGetImageFromCurrentImageContext() circularImage]; // 关闭上下文 UIGraphicsEndImageContext(); // 主队列回调 dispatch_async(dispatch_get_main_queue(), ^{ completion(resultImage); }); }); } - (UIImage *)circularImage { // 1. 开启图形上下文 UIGraphicsBeginImageContextWithOptions(self.size, NO, 0); // 2. 描述路径 UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.size.width, self.size.height) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(self.size.width, self.size.height)]; // UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, self.size.width, self.size.height)]; // 3. 添加裁减区域 [path addClip]; // 4. 绘制图片 [self drawAtPoint:CGPointZero]; // 5. 从上下文获取图片 UIImage * image = UIGraphicsGetImageFromCurrentImageContext(); // 6. 关闭上下文 UIGraphicsEndImageContext(); // 7. 设置图片 return image; } + (UIImage *)gradientColorImageFromColors:(NSArray *)colors gradientType:(GradientType)gradientType imgSize:(CGSize)imgSize{ NSMutableArray *ar = [NSMutableArray array]; for(UIColor *c in colors) { [ar addObject:(id)c.CGColor]; } UIGraphicsBeginImageContextWithOptions(imgSize, YES, 1); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]); CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL); CGPoint start; CGPoint end; switch (gradientType) { case GradientTypeTopToBottom: start = CGPointMake(0.0, 0.0); end = CGPointMake(0.0, imgSize.height); break; case GradientTypeLeftToRight: start = CGPointMake(0.0, 0.0); end = CGPointMake(imgSize.width, 0.0); break; case GradientTypeUpleftToLowright: start = CGPointMake(0.0, 0.0); end = CGPointMake(imgSize.width, imgSize.height); break; case GradientTypeUprightToLowleft: start = CGPointMake(imgSize.width, 0.0); end = CGPointMake(0.0, imgSize.height); break; default: break; } CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); CGGradientRelease(gradient); CGContextRestoreGState(context); CGColorSpaceRelease(colorSpace); UIGraphicsEndImageContext(); return image; } - (UIImage *)setCornerWithRadius:(CGFloat)radius andSize:(CGSize)size { //开启图形上下文 UIGraphicsBeginImageContext(size); //绘制圆角矩形 CGRect rect = CGRectMake(0, 0, size.width, size.height); UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)]; //将Path添加到上下文中 CGContextAddPath(UIGraphicsGetCurrentContext(), path.CGPath); //裁剪上下文 CGContextClip(UIGraphicsGetCurrentContext()); //将图片绘制到上下文中 [self drawInRect:rect]; //设置绘制模式 CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathStroke); //获取图片 UIImage *output = UIGraphicsGetImageFromCurrentImageContext(); //关闭上下文 UIGraphicsEndImageContext(); //返回裁剪好的图片 return output; } // 给图片添加图片水印 + (UIImage *)waterImageWithImage:(UIImage *)image waterImage:(UIImage *)waterImage waterImageRect:(CGRect)rect { //1.获取图片 //2.开启上下文 UIGraphicsBeginImageContextWithOptions(image.size, NO, 0); //3.绘制背景图片 [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)]; //绘制水印图片到当前上下文 [waterImage drawInRect:rect]; //4.从上下文中获取新图片 UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext(); //5.关闭图形上下文 UIGraphicsEndImageContext(); //返回图片 return newImage; } + (CGSize)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 *)cutImage:(CGSize)newSize{ CGFloat scale = newSize.height / self.size.height; UIImage *scaleImage = [self originImage:self scaleToSize:CGSizeMake(self.size.width*scale, self.size.height*scale)]; //裁剪暂时有问题 return scaleImage; } - (UIImage *)resizeTo:(CGSize)size { UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:size]; return [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull context) { [self drawInRect:CGRectMake(0, 0, size.width, size.height)]; }]; } - (UIImage *)cropRightAndBottomPixels:(NSUInteger)pixels { // 获取原图像的大小 CGSize originalSize = self.size; // 计算新的裁剪后的图像大小 CGSize newSize = CGSizeMake(originalSize.width - pixels, originalSize.height - pixels); // 开始图像上下文 UIGraphicsBeginImageContextWithOptions(newSize, NO, self.scale); // 绘制裁剪后的图像到上下文 [self drawAtPoint:CGPointZero]; // 获取裁剪后的图像 UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext(); // 结束图像上下文 UIGraphicsEndImageContext(); return croppedImage; } - (UIImage*) originImage:(UIImage *)image scaleToSize:(CGSize)size { UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale); [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; } -(UIImage *)compressWithMaxLength:(NSUInteger)maxLength{ // Compress by quality CGFloat compression = 1; NSData *data = UIImageJPEGRepresentation(self, compression); if (data.length < maxLength) return self; CGFloat max = 1; CGFloat min = 0; for (int i = 0; i < 6; ++i) { compression = (max + min) / 2; data = UIImageJPEGRepresentation(self, compression); //NSLog(@"Compression = %.1f", compression); //NSLog(@"In compressing quality loop, image size = %ld KB", data.length / 1024); if (data.length < maxLength * 0.9) { min = compression; } else if (data.length > maxLength) { max = compression; } else { break; } } //NSLog(@"After compressing quality, image size = %ld KB", data.length / 1024); if (data.length < maxLength) return self; UIImage *resultImage = [UIImage imageWithData:data]; // Compress by size NSUInteger lastDataLength = 0; while (data.length > maxLength && data.length != lastDataLength) { lastDataLength = data.length; CGFloat ratio = (CGFloat)maxLength / data.length; //NSLog(@"Ratio = %.1f", ratio); CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)), (NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank UIGraphicsBeginImageContext(size); [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)]; resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); data = UIImageJPEGRepresentation(resultImage, compression); //NSLog(@"In compressing size loop, image size = %ld KB", data.length / 1024); } if (data) { return [UIImage imageWithData:data];; } else { return self; } } - (UIImage *)roundedImageWithCornerRadius:(CGFloat)cornerRadius size:(CGSize)size{ UIGraphicsBeginImageContextWithOptions(size, NO, 1); UIBezierPath *clippingPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height) cornerRadius:cornerRadius]; [clippingPath addClip]; [self drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return roundedImage; } +(UIImage *)getImageFromView:(UIView *)view { // 1. 创建一个新的图像上下文,大小与view的bounds相匹配 UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0); // 2. 获取当前图像上下文 CGContextRef context = UIGraphicsGetCurrentContext(); // 3. 将view的层级渲染到上下文中 [view.layer renderInContext:context]; // 4. 从上下文中获取生成的图片 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); // 5. 结束图像上下文 UIGraphicsEndImageContext(); // 6. 返回生成的图片 return image; } + (NSString *)getImageTypeWithImageData:(NSData *)data { uint8_t c; [data getBytes:&c length:1]; switch (c) { case 0xFF: return @"jpeg"; case 0x89: return @"png"; case 0x47: return @"gif"; case 0x49: case 0x4D: return @"tiff"; case 0x52: if ([data length] < 12) { return nil; } NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding]; if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) { return @"webp"; } return nil; } return nil; } +(UIImage *)getLanguageImage:(NSString *)image{ NSString *curImage = image; NSString *language = [NSBundle getLanguageText]; if (isMSZH()) { // 不處理 } else if (isMSTR()) { image = [NSString stringWithFormat:@"%@_tr", image]; } else if (isMSRTL()) { image = [NSString stringWithFormat:@"%@_ar", image]; } else { image = [NSString stringWithFormat:@"%@_en", image]; } // 尝试获取带语言后缀的图片 UIImage *getImage = kImage(image); // 若图片不存在,尝试使用英语图片作为默认 if (getImage == nil) { NSString *defaultImageName = [NSString stringWithFormat:@"%@_en", curImage]; getImage = kImage(defaultImageName) ?: kImage(curImage); } return getImage; } +(NSString *)getLanguageText:(NSString *)image{ NSString *curImage = image; NSString *language = [NSBundle getLanguageText]; if ([language isEqualToString:@"en"]){ image = [NSString stringWithFormat:@"%@_en",image]; } else if ([language isEqualToString:@"ar"]){ image = [NSString stringWithFormat:@"%@_ar",image]; } else if ([language isEqualToString:@"tr"]) { // 土耳其语默认使用英语内容 image = [NSString stringWithFormat:@"%@_en",image]; } if (kImage(image) == nil){ return curImage; } return image; } - (UIImage *)imageByApplyingAlpha:(CGFloat)alpha { UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f); CGContextRef ctx = UIGraphicsGetCurrentContext(); CGRect area = CGRectMake(0, 0, self.size.width, self.size.height); CGContextScaleCTM(ctx, 1, -1); CGContextTranslateCTM(ctx, 0, -area.size.height); CGContextSetBlendMode(ctx, kCGBlendModeMultiply); CGContextSetAlpha(ctx, alpha); CGContextDrawImage(ctx, area, self.CGImage); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } @end