UIImage 各种处理(分类)
1 @interface UIImage (conversion) 2 3 //压缩图片到宽度1024 4 + (UIImage *)imageCompressSizeToMin1024Width:(UIImage *)image; 5 6 7 //修改图片size 8 + (UIImage *)image:(UIImage *)image byScalingToSize:(CGSize)targetSize; 9 10 //UIColor 转UIImage 11 + (UIImage *)imageWithColor: (UIColor*) color; 12 13 //图片转换成黑白(type1:灰 type2:橙 type3:蓝 other:不变) 14 + (UIImage *)grayscale:(UIImage*)anImage type:(int)type; 15 16 //view转图片 17 +(UIImage *)getImageFromView:(UIView *)view; 18 19 //图片转化成png 20 + (UIImage *)imageToPng:(UIImage *)image; 21 22 //图片转换成jpg格式 23 + (UIImage *)imageToJpg:(UIImage *)image; 24 25 //图片正向处理,相册读出来的图片有时候头不朝上 26 + (UIImage *)fixOrientation:(UIImage *)aImage; 27 28 @end
1 @implementation UIImage (conversion) 2 3 + (UIImage *)imageCompressSizeToMin1024Width:(UIImage *)image { 4 CGSize size = CGSizeZero; 5 if (image.size.width>1024) { 6 size.width = 1024; 7 size.height = image.size.height*1024/image.size.width; 8 image = [UIImage image:image byScalingToSize:size]; 9 } 10 return image; 11 } 12 13 /** 14 * 修改图片size 15 * 16 * @param image 原图片 17 * @param targetSize 要修改的size 18 * 19 * @return 修改后的图片 20 */ 21 + (UIImage *)image:(UIImage*)image byScalingToSize:(CGSize)targetSize { 22 UIImage *sourceImage = image; 23 UIImage *newImage = nil; 24 25 UIGraphicsBeginImageContext(targetSize); 26 27 CGRect thumbnailRect = CGRectZero; 28 thumbnailRect.origin = CGPointZero; 29 thumbnailRect.size.width = targetSize.width; 30 thumbnailRect.size.height = targetSize.height; 31 32 [sourceImage drawInRect:thumbnailRect]; 33 34 newImage = UIGraphicsGetImageFromCurrentImageContext(); 35 UIGraphicsEndImageContext(); 36 37 return newImage ; 38 } 39 40 //UIColor 转UIImage 41 + (UIImage *)imageWithColor: (UIColor*) color 42 { 43 CGRect rect=CGRectMake(0,0, 1, 1); 44 UIGraphicsBeginImageContext(rect.size); 45 CGContextRef context = UIGraphicsGetCurrentContext(); 46 CGContextSetFillColorWithColor(context, [color CGColor]); 47 CGContextFillRect(context, rect); 48 UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); 49 UIGraphicsEndImageContext(); 50 return theImage; 51 } 52 53 //图片转换成黑白 54 + (UIImage *)grayscale:(UIImage*)anImage type:(int)type { 55 //先转换成png 56 UIImage *pngImage = [UIImage imageToPng:anImage]; 57 58 CGImageRef imageRef = pngImage.CGImage; 59 60 size_t width = CGImageGetWidth(imageRef); 61 size_t height = CGImageGetHeight(imageRef); 62 63 size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef); 64 size_t bitsPerPixel = CGImageGetBitsPerPixel(imageRef); 65 66 size_t bytesPerRow = CGImageGetBytesPerRow(imageRef); 67 68 CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef); 69 70 CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); 71 72 73 bool shouldInterpolate = CGImageGetShouldInterpolate(imageRef); 74 75 CGColorRenderingIntent intent = CGImageGetRenderingIntent(imageRef); 76 77 CGDataProviderRef dataProvider = CGImageGetDataProvider(imageRef); 78 79 CFDataRef data = CGDataProviderCopyData(dataProvider); 80 81 UInt8 *buffer = (UInt8*)CFDataGetBytePtr(data); 82 83 NSUInteger x, y; 84 for (y = 0; y < height; y++) { 85 for (x = 0; x < width; x++) { 86 UInt8 *tmp; 87 tmp = buffer + y * bytesPerRow + x * 4; 88 89 UInt8 red,green,blue; 90 red = *(tmp + 0); 91 green = *(tmp + 1); 92 blue = *(tmp + 2); 93 94 UInt8 brightness; 95 switch (type) { 96 case 1: 97 brightness = (77 * red + 28 * green + 151 * blue) / 256; 98 *(tmp + 0) = brightness; 99 *(tmp + 1) = brightness; 100 *(tmp + 2) = brightness; 101 break; 102 case 2: 103 *(tmp + 0) = red; 104 *(tmp + 1) = green * 0.7; 105 *(tmp + 2) = blue * 0.4; 106 break; 107 case 3: 108 *(tmp + 0) = 255 - red; 109 *(tmp + 1) = 255 - green; 110 *(tmp + 2) = 255 - blue; 111 break; 112 default: 113 *(tmp + 0) = red; 114 *(tmp + 1) = green; 115 *(tmp + 2) = blue; 116 break; 117 } 118 } 119 } 120 CFDataRef effectedData = CFDataCreate(NULL, buffer, CFDataGetLength(data)); 121 CGDataProviderRef effectedDataProvider = CGDataProviderCreateWithCFData(effectedData); 122 CGImageRef effectedCgImage = CGImageCreate(width, height, 123 bitsPerComponent, bitsPerPixel, bytesPerRow, 124 colorSpace, bitmapInfo, effectedDataProvider, 125 NULL, shouldInterpolate, intent); 126 UIImage *effectedImage = [[UIImage alloc] initWithCGImage:effectedCgImage]; 127 CGImageRelease(effectedCgImage); 128 CFRelease(effectedDataProvider); 129 CFRelease(effectedData); 130 CFRelease(data); 131 132 return effectedImage; 133 134 } 135 136 //view转图片 137 + (UIImage *)getImageFromView:(UIView *)view{ 138 139 UIGraphicsBeginImageContext(view.bounds.size); 140 [view.layer renderInContext:UIGraphicsGetCurrentContext()]; 141 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 142 UIGraphicsEndImageContext(); 143 return image; 144 } 145 146 //图片转换成png格式 147 + (UIImage *)imageToPng:(UIImage *)image { 148 //我们需要将其转换成NSdata二进制存储, 149 NSData *data; 150 if (UIImagePNGRepresentation(image) == nil) { 151 data = UIImageJPEGRepresentation(image, 1); 152 } else { 153 data = UIImagePNGRepresentation(image); 154 } 155 NSFileManager *fileManager = [NSFileManager defaultManager]; 156 NSString *sandPath = NSHomeDirectory(); 157 //文件路径 158 NSString *path = [sandPath stringByAppendingPathComponent:@"tmp/image.png"]; 159 // DLog(@"%@",path); 160 [fileManager createFileAtPath:path contents:data attributes:nil]; // 将图片保存为PNG格式 161 [data writeToFile:path atomically:YES]; 162 UIImage *pngImage = [UIImage imageWithContentsOfFile:path]; 163 return pngImage; 164 } 165 166 //图片转换成jpg格式 167 + (UIImage *)imageToJpg:(UIImage *)image { 168 //我们需要将其转换成NSdata二进制存储, 169 NSData *data; 170 if (UIImagePNGRepresentation(image) == nil) { 171 data = UIImageJPEGRepresentation(image, 1); 172 } else { 173 data = UIImagePNGRepresentation(image); 174 } 175 176 NSFileManager *fileManager = [NSFileManager defaultManager]; 177 NSString *sandPath = NSHomeDirectory(); 178 //文件路径 179 NSString *path = [sandPath stringByAppendingPathComponent:@"tmp/image.jpg"]; 180 [fileManager createFileAtPath:path contents:data attributes:nil]; // 将图片保存为JPG格式 181 [data writeToFile:path atomically:YES]; 182 183 UIImage *jpgImage = [UIImage imageWithContentsOfFile:path]; 184 185 return jpgImage; 186 } 187 188 //正向处理 189 + (UIImage *)fixOrientation:(UIImage *)aImage { 190 191 // No-op if the orientation is already correct 192 if (aImage.imageOrientation ==UIImageOrientationUp) 193 return aImage; 194 195 // We need to calculate the proper transformation to make the image upright. 196 // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. 197 CGAffineTransform transform =CGAffineTransformIdentity; 198 199 switch (aImage.imageOrientation) { 200 case UIImageOrientationDown: 201 case UIImageOrientationDownMirrored: 202 transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height); 203 transform = CGAffineTransformRotate(transform, M_PI); 204 break; 205 206 case UIImageOrientationLeft: 207 case UIImageOrientationLeftMirrored: 208 transform = CGAffineTransformTranslate(transform, aImage.size.width,0); 209 transform = CGAffineTransformRotate(transform, M_PI_2); 210 break; 211 212 case UIImageOrientationRight: 213 case UIImageOrientationRightMirrored: 214 transform = CGAffineTransformTranslate(transform, 0, aImage.size.height); 215 transform = CGAffineTransformRotate(transform, -M_PI_2); 216 break; 217 default: 218 break; 219 } 220 221 switch (aImage.imageOrientation) { 222 case UIImageOrientationUpMirrored: 223 case UIImageOrientationDownMirrored: 224 transform = CGAffineTransformTranslate(transform, aImage.size.width,0); 225 transform = CGAffineTransformScale(transform, -1, 1); 226 break; 227 228 case UIImageOrientationLeftMirrored: 229 case UIImageOrientationRightMirrored: 230 transform = CGAffineTransformTranslate(transform, aImage.size.height,0); 231 transform = CGAffineTransformScale(transform, -1, 1); 232 break; 233 default: 234 break; 235 } 236 237 // Now we draw the underlying CGImage into a new context, applying the transform 238 // calculated above. 239 CGContextRef ctx =CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height, 240 CGImageGetBitsPerComponent(aImage.CGImage),0, 241 CGImageGetColorSpace(aImage.CGImage), 242 CGImageGetBitmapInfo(aImage.CGImage)); 243 CGContextConcatCTM(ctx, transform); 244 switch (aImage.imageOrientation) { 245 case UIImageOrientationLeft: 246 case UIImageOrientationLeftMirrored: 247 case UIImageOrientationRight: 248 case UIImageOrientationRightMirrored: 249 // Grr... 250 CGContextDrawImage(ctx,CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage); 251 break; 252 253 default: 254 CGContextDrawImage(ctx,CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage); 255 break; 256 } 257 258 // And now we just create a new UIImage from the drawing context 259 CGImageRef cgimg =CGBitmapContextCreateImage(ctx); 260 UIImage *img = [UIImage imageWithCGImage:cgimg]; 261 CGContextRelease(ctx); 262 CGImageRelease(cgimg); 263 return img; 264 } 265 266 @end
mark -- 1. 对照片做处理的时候 先旋转方向 再改变格式
2.如果拍设的照片太大的话上传后台估计会出错
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info { UIImage *originalImage = (UIImage *)[info objectForKey:UIImagePickerControllerEditedImage]; if (!originalImage) { originalImage = (UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage]; } UIImage *imga = [UIImage fixOrientation:originalImage];//图片正向处理 UIImage *img = [UIImage imageToJpg:imga];//转换成jpg NSData *imageData = UIImageJPEGRepresentation(img, 1); if (imageData.length>300000){ UIImage *image = [UIImage imageWithData:imageData]; imageData = UIImageJPEGRepresentation(image, 0.005); } NSString *encodedImageStr = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; [picker dismissViewControllerAnimated:YES completion:nil]; }