UIImage常用的分类
来源:互联网 发布:js获取class 编辑:程序博客网 时间:2024/06/06 02:43
有些时候经常需要对系统本有的类方法进行扩充,于是可能需要对类采用继承或者分类的方式来实现需要。
最近写的一个项目用到了一些对图片进行处理的一些扩充方法。
涉及到的方法:
比较常用的类似于拉伸图片,拼接图片名称或者以颜色生成图片等。
拼接图片名称:
- + (UIImage *)imageMatchSizeWithName:(NSString *)imageName
- {
- if (__Device_Iphone_5__) //iphone5,5s
- {
- NSString *ext = [imageName pathExtension];
- imageName = [imageName stringByDeletingPathExtension];
- imageName = [imageName stringByAppendingString:@"-568h@2x"];
- imageName = [imageName stringByAppendingPathExtension:ext];
- }
- return [UIImage imageNamed:imageName];
- }
通过宏来给图片名称拼接一个4寸标识。
拉伸图片到指定尺寸:
- + (UIImage *)compressImage:(UIImage *)imgSrc toSize:(CGSize)size
- {
- UIGraphicsBeginImageContext(size);
- CGRect rect = {{0,0}, size};
- [imgSrc drawInRect:rect];
- UIImage *compressedImg = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return compressedImg;
- }
通过上下文来绘制实现将图片拉伸到指定的尺寸。
指定位置获取像素点平铺拉伸图片
- + (UIImage *)strechImageWithName:(NSString *)imageName
- {
- UIImage *image = [UIImage imageNamed:imageName];
- return [image stretchableImageWithLeftCapWidth:image.size.width*0.5 topCapHeight:image.size.height*0.5];
- }
- + (UIImage *)strechImageWithName:(NSString *)imageName posX:(CGFloat)x posY:(CGFloat)y
- {
- UIImage *image = [UIImage imageNamed:imageName];
- return [image stretchableImageWithLeftCapWidth:image.size.width*x topCapHeight:image.size.height*y];
- }
这里涉及到一个端帽,不过如果不清楚只要知道是根据指定的像素位置平铺来实现拉伸即可。
截屏:
- + (UIImage *)screenshot
- {
- CGSize imageSize = [[UIScreen mainScreen] bounds].size;
- if (NULL != UIGraphicsBeginImageContextWithOptions) {
- UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
- } else {
- UIGraphicsBeginImageContext(imageSize);
- }
- CGContextRef context = UIGraphicsGetCurrentContext();
- for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
- if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {
- CGContextSaveGState(context);
- CGContextTranslateCTM(context, [window center].x, [window center].y);
- CGContextConcatCTM(context, [window transform]);
- CGContextTranslateCTM(context,
- -[window bounds].size.width * [[window layer] anchorPoint].x,
- -[window bounds].size.height * [[window layer] anchorPoint].y);
- [[window layer] renderInContext:context];
- CGContextRestoreGState(context);
- }
- }
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return image;
- }
增加水印:
- + (UIImage *)addImage:(UIImage *)image addMsakImage:(UIImage *)maskImage maskFrame:(CGRect)pos
- {
- UIGraphicsBeginImageContext(image.size);
- [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
- //水印图片的位置
- [maskImage drawInRect:pos];
- UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return resultingImage;
- }
指定颜色生成图片:
- + (UIImage *)imageWithColor:(UIColor *)color
- {
- CGRect rect = CGRectMake(0, 0, 1, 1);
- UIGraphicsBeginImageContext(rect.size);
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextSetFillColorWithColor(context, [color CGColor]);
- CGContextFillRect(context, rect);
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return image;
- }
以上三个方法其实均有些类似,都是通过图片上下文来进行操作。
相对于上述方法,进行模糊稍微麻烦一些,也可以使用CoreImage中的高斯模糊滤镜,CoreImage的简单使用可以参照之前的博客:
CoreImage的使用及常见滤镜工具(一)
- + (UIImage *)blurImage:(UIImage *)src amount:(CGFloat)amount
- {
- if (amount < 0.0 || amount > 1.0) {
- amount = 0.5;
- }
- int boxSize = (int)(amount * 40);
- boxSize = boxSize - (boxSize % 2) + 1;
- CGImageRef img = src.CGImage;
- vImage_Buffer inBuffer, outBuffer;
- vImage_Error error;
- voidvoid *pixelBuffer;
- CGDataProviderRef inProvider = CGImageGetDataProvider(img);
- CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
- inBuffer.width = CGImageGetWidth(img);
- inBuffer.height = CGImageGetHeight(img);
- inBuffer.rowBytes = CGImageGetBytesPerRow(img);
- inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
- pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
- outBuffer.data = pixelBuffer;
- outBuffer.width = CGImageGetWidth(img);
- outBuffer.height = CGImageGetHeight(img);
- outBuffer.rowBytes = CGImageGetBytesPerRow(img);
- error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
- if (!error) {
- error = vImageBoxConvolve_ARGB8888(&outBuffer, &inBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
- if (!error) {
- error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
- }
- }
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
- CGContextRef ctx = CGBitmapContextCreate(outBuffer.data,
- outBuffer.width,
- outBuffer.height,
- 8,
- outBuffer.rowBytes,
- colorSpace,
- (CGBitmapInfo)kCGImageAlphaNoneSkipLast);
- CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
- UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
- CGContextRelease(ctx);
- CGColorSpaceRelease(colorSpace);
- free(pixelBuffer);
- CFRelease(inBitmapData);
- CGColorSpaceRelease(colorSpace);
- CGImageRelease(imageRef);
- return returnImage;
- }
实现即使不是太清楚,也可以直接拿来用就行。
资源位置
GitHub:UIImage-HR
CSDN:iOS图片分类
0 0
- UIImage常用的分类
- UIImage常用的分类
- UIImage常用的分类
- UIImage的分类
- iOS开发中常用的分类方法---UIImage+extension
- 生成缩略图的UIImage分类
- Quartz 2D中UIImage的分类
- 进阶!Quartz2D对UIImage的常用操作
- iphone UIImage常用方法
- uiimage常用方法总结
- iPhone中对UIImage对象处理的常用方法
- IOS UIImage常用的加载图片有3种方式
- iOS最不卡顿的圆角图片的设置(UIImage分类)
- UIImage+GIF.swift SDWebImage中处理GIF的分类的swift版实现
- java常用的Listener分类
- java常用的Listener分类
- 常用数据结构的一部分类
- iOS App 常用的分类
- 获取iphone的硬件版本以及系统信息
- Android EditText 禁止换行
- oracle官方文档之SQL语句语法的图形表示法
- Android 保存图片到系统及相关问题的解决方案
- 免费打电话。
- UIImage常用的分类
- rabbitmq连接被阻塞,无法接收消息
- 算法之旅,直奔pair
- Google Chrome Source Code 浏览器源码(下载)
- d3学习之(Data Visualization with d3.js Cookbook )二(第二章)
- 弹出dialog 窗口; 以及在dialog 中关闭并且刷新父窗体的方法
- 分布式服务框架 Zookeeper -- 管理分布式环境中的数据
- android桌面组件开发
- Linux NFS服务器的安装与配置