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
.h
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
.m
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]; }
转载于:https://www.cnblogs.com/xuaninitial/p/6641551.html
UIImage 各种处理(分类)相关推荐
- UIImage常用的分类
有些时候经常需要对系统本有的类方法进行扩充,于是可能需要对类采用继承或者分类的方式来实现需要. 最近写的一个项目用到了一些对图片进行处理的一些扩充方法. 涉及到的方法: 比较常用的类似于拉伸图片,拼接 ...
- 源码阅读:SDWebImage(十九)——UIImage+ForceDecode/UIImage+GIF/UIImage+MultiFormat
该文章阅读的SDWebImage的版本为4.3.3. 由于这几个分类都是UIImage的分类,并且内容相对较少,就写在一篇文章中. 1.UIImage+ForceDecode 这个分类为UIImage ...
- SDWebImage源码阅读(三)UIImage+GIF
UIImage+GIF 是UIImage 类的一个GIF 分类,在之前的版本里面这个分类是用了处理GIF 动态图片的但是会有内存暴增的bug.在当前 '4.0.0-beta2' 的版本里GIF 动态图 ...
- [Runtime]-交换方法
为什么80%的码农都做不了架构师?>>> 1:比如我们有个需求:让UIImage加载图片的时候,告诉我们是否加载成功了 前言: /** Runtime(交换方法):只要是想修 ...
- Runtime 总结
参考文章 1. `文/滕先洪(简书作者)原文链接:http://www.jianshu.com/p/ab966e8a82e2著作权归作者所有,转载请联系作者获得授权,并标注"简书作者&quo ...
- iOS开发使用半透明模糊效果方法整理
转载:http://www.cocoachina.com/ios/20141223/10731.html 虽然iOS很早就支持使用模糊效果对图片等进行处理,但尤其在iOS7以后,半透明模糊效果得到大范 ...
- 百思不得姐第4天:文本框占位文字颜色
一:设置登录界面和注册界面的切换 #import "CQLoginViewController.h" #import "CQCustomTextField.h" ...
- AFNetworking 3.0源码阅读 - AFURLResponseSerialization
这次来说一下AFURLResponseSerialization这个HTTP响应类. 定义了一个协议,该协议返回序列化后的结果.后续的AFHTTPResponseSerializer以及他的子类都遵循 ...
- java bean set_JavaBean自动生成get和set方法
android计算每个目录剩余空间丶总空间以及SD卡剩余空间 ublic class MemorySpaceCheck { /** * 计算剩余空间 * @param path * @return * ...
- [转]iOS开发使用半透明模糊效果方法整理
转自:http://www.molotang.com/articles/1921.html 虽然iOS很早就支持使用模糊效果对图片等进行处理,但尤其在iOS7以后,半透明模糊效果得到大范围广泛使用.包 ...
最新文章
- mysql语句生成工具_强烈推荐一款MySQL语句优化辅助工具,值得收藏
- P7920-[Kubic]Permutation
- mysql memory=off_MySQL内存调优
- linux awk 日志分析,Linux Awk使用案例总结 nginx日志统计
- Element type quot;Resourcequot; must be followed by either attribute specifications, quot;gt;qu...
- java实现关键词云_Java synchronized 关键字的实现原理
- 20191010:希尔排序代码详解
- [转载] Python大数据文本分析及应用
- 嵌入式根文件系统的移植和制作详解【转】
- 阿帕虎题库管理系统服务器,阿帕虎题库管理系统
- Unipus-writing exercise Expository_Text_09
- java模拟简单的qq聊天_初学java之模拟QQ聊天软件(简单实现)
- PHP生成订单号算法
- PS练习7——蒙版的使用
- 中国移动互联网第一次大战盘点:360让别人不赚钱的能力很强
- 【算法】判断一个点是否在多边形之内
- day03--java基础编程:面向对象,构造方法,代码块讲解,this super,static,final,访问修饰符,方法重写,向上/下造型,main方法,抽象类,接口,设计模式,异常,内部类
- 《GAMES104-现代游戏引擎:从入门到实践》-02 学习笔记
- RuntimeError: Could not infer dtype of NoneType
- 服务器c盘哪些文件可以删,教你c盘哪些文件可以删除
热门文章
- android post请求_Vue 网络请求框架 axios 使用教程
- swift文件服务器,Swift3一行代码将各种类型文件上传到服务器
- 批量替换_批量替换图框
- you need python_Life is short,you need Python——Python序列(列表)
- pandas内置数据集_pandas内置数据集_Pandas中的示例数据集
- 快来学习Redis 分布式锁的背后原理
- PCL:求两条直线交点
- 2014.4新版uboot启动流程分析
- 【机器学习入门】(12) 特征工程:特征选择、数据降维、PCA
- C语言实现bmp图像对比度扩展