UIImage常用的分类

有些时候经常需要对系统本有的类方法进行扩充,于是可能需要对类采用继承或者分类的方式来实现需要。

最近写的一个项目用到了一些对图片进行处理的一些扩充方法。


涉及到的方法:



比较常用的类似于拉伸图片,拼接图片名称或者以颜色生成图片等。


拼接图片名称:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. + (UIImage *)imageMatchSizeWithName:(NSString *)imageName  
  2. {  
  3.     if (__Device_Iphone_5__)       //iphone5,5s  
  4.     {  
  5.         NSString *ext = [imageName pathExtension];  
  6.           
  7.         imageName = [imageName stringByDeletingPathExtension];  
  8.         imageName = [imageName stringByAppendingString:@"-568h@2x"];  
  9.         imageName = [imageName stringByAppendingPathExtension:ext];  
  10.     }  
  11.       
  12.     return [UIImage imageNamed:imageName];  
  13. }  

通过宏来给图片名称拼接一个4寸标识。


拉伸图片到指定尺寸:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. + (UIImage *)compressImage:(UIImage *)imgSrc toSize:(CGSize)size  
  2. {  
  3.     UIGraphicsBeginImageContext(size);  
  4.     CGRect rect = {{0,0}, size};  
  5.     [imgSrc drawInRect:rect];  
  6.     UIImage *compressedImg = UIGraphicsGetImageFromCurrentImageContext();  
  7.     UIGraphicsEndImageContext();  
  8.     return compressedImg;  
  9. }  

通过上下文来绘制实现将图片拉伸到指定的尺寸。


指定位置获取像素点平铺拉伸图片

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. + (UIImage *)strechImageWithName:(NSString *)imageName  
  2. {  
  3.     UIImage *image = [UIImage imageNamed:imageName];  
  4.       
  5.     return [image stretchableImageWithLeftCapWidth:image.size.width*0.5 topCapHeight:image.size.height*0.5];  
  6. }  
  7.   
  8. + (UIImage *)strechImageWithName:(NSString *)imageName posX:(CGFloat)x posY:(CGFloat)y  
  9. {  
  10.     UIImage *image = [UIImage imageNamed:imageName];  
  11.       
  12.     return [image stretchableImageWithLeftCapWidth:image.size.width*x topCapHeight:image.size.height*y];  
  13. }  

这里涉及到一个端帽,不过如果不清楚只要知道是根据指定的像素位置平铺来实现拉伸即可。


截屏:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. + (UIImage *)screenshot  
  2. {  
  3.     CGSize imageSize = [[UIScreen mainScreen] bounds].size;  
  4.       
  5.     if (NULL != UIGraphicsBeginImageContextWithOptions) {  
  6.         UIGraphicsBeginImageContextWithOptions(imageSize, NO0);  
  7.     } else {  
  8.         UIGraphicsBeginImageContext(imageSize);  
  9.     }  
  10.       
  11.     CGContextRef context = UIGraphicsGetCurrentContext();  
  12.       
  13.     for (UIWindow *window in [[UIApplication sharedApplication] windows]) {  
  14.         if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {  
  15.             CGContextSaveGState(context);  
  16.               
  17.             CGContextTranslateCTM(context, [window center].x, [window center].y);  
  18.               
  19.             CGContextConcatCTM(context, [window transform]);  
  20.               
  21.             CGContextTranslateCTM(context,  
  22.                                   -[window bounds].size.width * [[window layer] anchorPoint].x,  
  23.                                   -[window bounds].size.height * [[window layer] anchorPoint].y);  
  24.               
  25.             [[window layer] renderInContext:context];  
  26.               
  27.             CGContextRestoreGState(context);  
  28.         }  
  29.     }  
  30.       
  31.     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();  
  32.       
  33.     UIGraphicsEndImageContext();  
  34.       
  35.     return image;  
  36. }  


增加水印:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. + (UIImage *)addImage:(UIImage *)image addMsakImage:(UIImage *)maskImage maskFrame:(CGRect)pos  
  2. {  
  3.     UIGraphicsBeginImageContext(image.size);  
  4.     [image drawInRect:CGRectMake(00, image.size.width, image.size.height)];  
  5.       
  6.     //水印图片的位置  
  7.     [maskImage drawInRect:pos];  
  8.     UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();  
  9.     UIGraphicsEndImageContext();  
  10.     return resultingImage;  
  11. }  


指定颜色生成图片:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. + (UIImage *)imageWithColor:(UIColor *)color  
  2. {  
  3.     CGRect rect = CGRectMake(0011);  
  4.     UIGraphicsBeginImageContext(rect.size);  
  5.     CGContextRef context = UIGraphicsGetCurrentContext();  
  6.       
  7.     CGContextSetFillColorWithColor(context, [color CGColor]);  
  8.     CGContextFillRect(context, rect);  
  9.       
  10.     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();  
  11.     UIGraphicsEndImageContext();  
  12.       
  13.     return image;  
  14. }  

以上三个方法其实均有些类似,都是通过图片上下文来进行操作。


相对于上述方法,进行模糊稍微麻烦一些,也可以使用CoreImage中的高斯模糊滤镜,CoreImage的简单使用可以参照之前的博客:

CoreImage的使用及常见滤镜工具(一)

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. + (UIImage *)blurImage:(UIImage *)src amount:(CGFloat)amount  
  2. {  
  3.     if (amount < 0.0 || amount > 1.0) {  
  4.         amount = 0.5;  
  5.     }  
  6.       
  7.     int boxSize = (int)(amount * 40);  
  8.     boxSize = boxSize - (boxSize % 2) + 1;  
  9.       
  10.     CGImageRef img = src.CGImage;  
  11.       
  12.     vImage_Buffer inBuffer, outBuffer;  
  13.     vImage_Error error;  
  14.       
  15.     voidvoid *pixelBuffer;  
  16.       
  17.     CGDataProviderRef inProvider = CGImageGetDataProvider(img);  
  18.     CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);  
  19.       
  20.     inBuffer.width = CGImageGetWidth(img);  
  21.     inBuffer.height = CGImageGetHeight(img);  
  22.     inBuffer.rowBytes = CGImageGetBytesPerRow(img);  
  23.       
  24.     inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);  
  25.       
  26.     pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));  
  27.       
  28.     outBuffer.data = pixelBuffer;  
  29.     outBuffer.width = CGImageGetWidth(img);  
  30.     outBuffer.height = CGImageGetHeight(img);  
  31.     outBuffer.rowBytes = CGImageGetBytesPerRow(img);  
  32.       
  33.     error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL00, boxSize, boxSize, NULL, kvImageEdgeExtend);  
  34.       
  35.     if (!error) {  
  36.         error = vImageBoxConvolve_ARGB8888(&outBuffer, &inBuffer, NULL00, boxSize, boxSize, NULL, kvImageEdgeExtend);  
  37.           
  38.         if (!error) {  
  39.             error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL00, boxSize, boxSize, NULL, kvImageEdgeExtend);  
  40.         }  
  41.     }  
  42.       
  43.     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();  
  44.       
  45.     CGContextRef ctx = CGBitmapContextCreate(outBuffer.data,  
  46.                                              outBuffer.width,  
  47.                                              outBuffer.height,  
  48.                                              8,  
  49.                                              outBuffer.rowBytes,  
  50.                                              colorSpace,  
  51.                                              (CGBitmapInfo)kCGImageAlphaNoneSkipLast);  
  52.       
  53.     CGImageRef imageRef = CGBitmapContextCreateImage (ctx);  
  54.       
  55.     UIImage *returnImage = [UIImage imageWithCGImage:imageRef];  
  56.       
  57.     CGContextRelease(ctx);  
  58.     CGColorSpaceRelease(colorSpace);  
  59.       
  60.     free(pixelBuffer);  
  61.     CFRelease(inBitmapData);  
  62.       
  63.     CGColorSpaceRelease(colorSpace);  
  64.     CGImageRelease(imageRef);  
  65.       
  66.     return returnImage;  
  67. }  
需要注意的是,这个方法需要导入系统库:<Accelerate/Accelerate.h>


实现即使不是太清楚,也可以直接拿来用就行。

资源位置

GitHub:UIImage-HR

CSDN:iOS图片分类



以上就是本篇博客全部内容,欢迎指正和交流。转载注明出处~http://blog.csdn.net/cocoarannie/article/details/18566043

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值