先罗列一下工具类中提供的方法:

/***  根据原始view和毛玻璃样式,获取模糊视图,并自动作为原view的subview(如果不需要作为子视图,自行调用removeFromSuperview)*/
+ (UIView *)getBlurEffectViewWithOriginalView:(UIView *)originalView style:(ImageHelperBlurEffectStyle)style;/***  根据原始图像和毛玻璃样式,获取新图像*/
+ (UIImage *)getBlurEffectImageWithOriginalImage:(UIImage *)originalImage style:(ImageHelperBlurEffectStyle)style;/***  根据原始图像,等比缩放系数,得到新图像*/
+ (UIImage *)getImageWithOriginalImage:(UIImage *)originalImage scale:(CGFloat)scale;/***  根据原始图像,等比缩放最大尺寸,得到新图像*/
+ (UIImage *)getImageWithOriginalImage:(UIImage *)originalImage scaleMaxSize:(CGSize)scaleMaxSize;/***  根据原始图像,等比缩放最大尺寸,得到新尺寸*/
+ (CGSize)getImageSizeWithOriginalImage:(UIImage *)originalImage scaleMaxSize:(CGSize)scaleMaxSize;/***  根据原始图像,完全填充尺寸,得到新图像*/
+ (UIImage *)getImageWithOriginalImage:(UIImage *)originalImage fillSize:(CGSize)fillSize;/***  根据原始图像,裁剪区域,得到新图像*/
+ (UIImage *)getImageWithOriginalImage:(UIImage *)originalImage cutFrame:(CGRect)cutFrame;/***  根据颜色,得到单位尺寸的纯色新图像*/
+ (UIImage *)getImageWithColor:(UIColor *)color;/***  根据view,得到快照*/
+ (UIImage *)getSnapshotWithView:(UIView *)view;/***  全屏截图,但不包括状态栏*/
+ (UIImage *)getFullScreenSnapshot;

说明:

1.获取毛玻璃效果(高斯模糊)图像

在iOS8以后,提供了类UIBlurEffect、UIVisualEffectView,可以方便的生成高斯模糊的视图,然后只需要作为目标视图的subview即可看到效果。在iOS7上需要自行实现,不过苹果在WWDC 2013上提供了一个UIImage+ImageEffects的分类,可以生成高斯模糊的图像。将分类加入项目Categories目录后,在ImageHelper中引用。因为UIBlurEffectStyle是在iOS8以后出现的,所以自定义了一个与其对应的枚举类型ImageHelperBlurEffectStyle,以便在iOS7中也可以正常使用。实现代码如下:

+ (UIView *)getBlurEffectViewWithOriginalView:(UIView *)originalView style:(ImageHelperBlurEffectStyle)style
{if (DeviceIOSVersionAbove(8)) {UIBlurEffectStyle blurStyle;switch (style) {case ImageHelperBlurEffectStyleExtraLight: {blurStyle = UIBlurEffectStyleExtraLight;break;}case ImageHelperBlurEffectStyleLight: {blurStyle = UIBlurEffectStyleLight;break;}case ImageHelperBlurEffectStyleDark: {blurStyle = UIBlurEffectStyleDark;break;}}UIBlurEffect *effect = [UIBlurEffect effectWithStyle:blurStyle];UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];effectView.frame = originalView.bounds;[originalView addSubview:effectView];return effectView;} else {UIImage *originalImage = [self getSnapshotWithView:originalView];UIImage *blurImage = [self getBlurEffectImageWithOriginalImage:originalImage style:style];UIImageView *effectView = [[UIImageView alloc] initWithFrame:originalView.bounds];[effectView setImage:blurImage];[originalView addSubview:effectView];return effectView;}
}+ (UIImage *)getBlurEffectImageWithOriginalImage:(UIImage *)originalImage style:(ImageHelperBlurEffectStyle)style
{UIImage *newImage;switch (style) {case ImageHelperBlurEffectStyleExtraLight: {newImage = [originalImage applyExtraLightEffect];break;}case ImageHelperBlurEffectStyleLight: {newImage = [originalImage applyLightEffect];break;}case ImageHelperBlurEffectStyleDark: {newImage = [originalImage applyDarkEffect];break;}}return newImage;
}

2.提供了将图像等比例缩放的系列方法,以及裁剪的方法。基本思路就是在当前图像上下文中,指定绘制尺寸(即位图的尺寸),然后将相应图像绘制到指定位置,然后生成最终图像。例如裁剪图像示例代码:

+ (UIImage *)getImageWithOriginalImage:(UIImage *)originalImage cutFrame:(CGRect)cutFrame
{CGSize newSize = cutFrame.size;UIGraphicsBeginImageContext(newSize);[originalImage drawInRect:CGRectMake(-cutFrame.origin.x, -cutFrame.origin.y, cutFrame.size.width, cutFrame.size.height)];UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return newImage;
}

3.截屏方法

需要用到方法:

UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)。第一个参数仍然是指定图像上下文绘制尺寸;第二个参数指定是否不透明;第三个为等比例缩放系数,如果为0.0,表示与设备主屏幕的系数一致。

CALayer的renderInContext:(CGContextRef)ctx方法,将图层全部渲染到某个上下文中,建议为当前图像上下文。最后得到图像。

+ (UIImage *)getSnapshotWithView:(UIView *)view
{UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);[view.layer renderInContext:UIGraphicsGetCurrentContext()];UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return newImage;
}+ (UIImage *)getFullScreenSnapshot
{return [self getSnapshotWithView:[UIApplication sharedApplication].keyWindow];
}

全屏截图时候,不包括状态栏,因为状态栏不在应用的window上,无法直接获取。但是可以通过私有Api拿到系统截屏图像,在此不展开探究。

测试内容

    UIImage *icon = LOADIMAGE(AppIcon);UIImage *testImg;testImg = [ImageHelper getImageWithOriginalImage:icon scale:2];LOG(@"%@", testImg);testImg = [ImageHelper getImageWithOriginalImage:icon scaleMaxSize:CGSizeMake(100, 90)];LOG(@"%@", testImg);testImg = [ImageHelper getImageWithOriginalImage:icon fillSize:CGSizeMake(100, 90)];LOG(@"%@", testImg);testImg = [ImageHelper getImageWithOriginalImage:icon cutFrame:CGRectMake(10, 10, 50, 50)];LOG(@"%@", testImg);testImg = [ImageHelper getImageWithColor:COLOR(255, 120, 100)];LOG(@"%@", testImg);testImg = [ImageHelper getSnapshotWithView:self.view];LOG(@"%@", testImg);testImg = [ImageHelper getFullScreenSnapShot];LOG(@"%@", testImg);testImg = [ImageHelper getBlurEffectImageWithOriginalImage:testImg style:ImageHelperBlurEffectStyleDark];LOG(@"%@", testImg);UIView *coverView = [ImageHelper getBlurEffectViewWithOriginalView:[UIApplication sharedApplication].keyWindow style:ImageHelperBlurEffectStyleDark];

2016-09-13 19:05:11.995 base[33087:2301853] <UIImage: 0x7ffaf97e8d00>, {120, 120}
2016-09-13 19:05:11.997 base[33087:2301853] <UIImage: 0x7ffaf97e9610>, {90, 90}
2016-09-13 19:05:11.999 base[33087:2301853] <UIImage: 0x7ffaf950a330>, {100, 90}
2016-09-13 19:05:12.001 base[33087:2301853] <UIImage: 0x7ffaf9463630>, {50, 50}
2016-09-13 19:05:12.002 base[33087:2301853] <UIImage: 0x7ffaf950a330>, {1, 1}
2016-09-13 19:05:12.007 base[33087:2301853] <UIImage: 0x7ffaf96004b0>, {375, 667}
2016-09-13 19:05:12.013 base[33087:2301853] <UIImage: 0x7ffaf950a330>, {375, 667}
2016-09-13 19:05:12.040 base[33087:2301853] <UIImage: 0x7ffaf9506f30>, {375, 667}

1.可以单步调试代码,查看testImg图像内容:

2.[ImageHelper getSnapshotWithView:self.view];这行代码中的self.view生成快照后,尺寸为{375, 667},但如果在viewDidLoad方法中加入代码:

self.edgesForExtendedLayout = UIRectEdgeNone;

重新输出的尺寸为{375, 603},因为edgesForExtendedLayout属性默认为UIRectEdgeAll,这在处理UI布局时候需要注意。后续记录的UIViewContoller父类也会提到。

base项目已更新:git@github.com:ALongWay/base.git

======================================

2016.09.14更新

1.修改了UIGraphicsBeginImageContext为UIGraphicsBeginImageContextWithOptions方法,设置不透明为NO,scale系数为0.0。即处理后为高质量的图像,包括原图alpha值。截屏方法区别在于没有包括透明度值

2.图像裁剪方法更新:

+ (UIImage *)getImageWithOriginalImage:(UIImage *)originalImage cutFrame:(CGRect)cutFrame
{CGImageRef cgimageRef = CGImageCreateWithImageInRect(originalImage.CGImage, cutFrame);UIImage *newImage = [UIImage imageWithCGImage:cgimageRef];CGImageRelease(cgimageRef);return newImage;
}

裁剪区域可以任意设定。之前的方法,当偏移区域加上裁剪区域没有覆盖原图全部区域时候,图像在上下文中绘制会失败。

3.增加了图像合并方法,可以合并任意数量的图像

+ (UIImage *)getImageMergedWithOriginalImageArray:(NSArray<ImageHelperMergeImage *> *)imageArray
{if (!imageArray|| imageArray.count == 0) {return nil;}ImageHelperMergeImage *firstMergeImage = [imageArray firstObject];//将第一张图作为背景放置CGRect firstMergeRect = firstMergeImage.mergeRect;firstMergeRect.origin = CGPointZero;firstMergeImage.mergeRect = firstMergeRect;UIGraphicsBeginImageContextWithOptions(firstMergeImage.mergeRect.size, NO, 0.0);for (ImageHelperMergeImage *mergeImage in imageArray) {[mergeImage.image drawInRect:mergeImage.mergeRect];}UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return newImage;
}

定义了一个合并图像的对象ImageHelperMergeImage,有一个UIImage属性存储图像,和CGRect属性储存合并的位置。

===================================

2016.09.23更新

之前的全屏截图方法,受限于状态栏无法获取,导致截图无状态栏。

后来,打印了一下UIApplication的全部私有变量和方法,找到一个名为“_statusBar”的私有变量,类型名为UIStatusBar。

猜测为UIView的子类,使用getSnapshotWithView:方法,得到了一个(375,20)的黑条,将方法中代码:

UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);

第二个参数修改为NO,得到了想要的状态栏截图:

代码如下:

+ (UIImage *)getFullScreenSnapshotWithStatusBar
{
//    [StringHelper printAllPrivateVariablesAndMethodsWithClassName:@"UIApplication"];
    UIApplication *app = [UIApplication sharedApplication];//私有变量得到状态栏UIView *statusBar = [app valueForKeyPath:@"statusBar"];UIImage *statusBarImage = [self getSnapshotWithView:statusBar];UIImage *bgImage = [self getFullScreenSnapshotWithoutStatusBar];ImageHelperMergeImage *mergeImage1 = [ImageHelperMergeImage getImageHelperMergeImageWithImage:bgImage];ImageHelperMergeImage *mergeImage2 = [ImageHelperMergeImage getImageHelperMergeImageWithImage:statusBarImage];UIImage *newImage = [self getImageMergedWithOriginalImageArray:@[mergeImage1, mergeImage2]];return newImage;
}

base项目已更新:git@github.com:ALongWay/base.git

转载于:https://www.cnblogs.com/ALongWay/p/5869561.html

App开发流程之图像处理工具类相关推荐

  1. App开发流程之加密工具类

    从这篇记录开始,记录的都算是干货了,都是一些编程日常的积累. 我建议先将基础的工具加入项目,后续的开发效率会呈指数增长.如果在专注功能开发过程中,才发现缺少大量常用的工具,不仅会打断思路,还会拖慢开发 ...

  2. iOS 直播类APP开发流程

    (一) iOS 直播类APP开发流程分解: 1 . 音视频处理的一般流程: 数据采集→数据编码→数据传输(流媒体服务器) →解码数据→播放显示 1.数据采集: 摄像机及拾音器收集视频及音频数据,此时得 ...

  3. (转载)iOS直播类APP开发流程

    转载自博主:iOS_developer_zhong,博客地址: http://blog.csdn.net/zhonggaorong/article/details/51483282 本文为大家分享了i ...

  4. 图像处理工具类、Bitmap处理、理解ThumbnailUtils

    在实际项目中,我们经常会遇到处理各种各样的图片问题. 比如:图片的旋转.缩放.图片格式转换.获取图片类型.验证图片大小.写入图片 等. 这里我们使用Java.awt.Graphics2D来实现常用图像 ...

  5. android开发监听媒体播放器,Android开发之媒体播放工具类完整示例

    本文实例讲述了Android开发之媒体播放工具类.分享给大家供大家参考,具体如下: package com.maobang.imsdk.util; import android.media.Media ...

  6. 开发安卓app游戏_「安卓APP开发流程」安卓APP如何开发的?

    21世纪,智能手机走进了人们的生活,现在的智能手机的操作系统基本分为两种,一种是IOS系统(苹果系统).安卓系统,其中,安卓系统是开源的,所以很多品牌商会讲安卓包装成自己的系统,但核心还是一样的,都是 ...

  7. APP开发流程实例讲解-儒释道网络电台八天开发全程-百度云深度兼容测试并进一步优化排错

    APP开发流程实例讲解-儒释道网络电台八天开发全程之 百度云深度兼容测试并进一步优化排错 APP开发流程实例讲解-儒释道网络电台八天开发全程 项目发起 功能和界面初步设定 在Android Studi ...

  8. 安卓app开发方案_「安卓APP开发流程」安卓APP如何开发的?

    21世纪,智能手机走进了人们的生活,现在的智能手机的操作系统基本分为两种,一种是IOS系统(苹果系统).安卓系统,其中,安卓系统是开源的,所以很多品牌商会讲安卓包装成自己的系统,但核心还是一样的,都是 ...

  9. APP开发流程实例讲解-儒释道网络电台八天开发全程-功能和界面初步设定

    APP开发流程实例讲解-儒释道网络电台八天开发全程 能和界面初步设定 APP开发流程实例讲解-儒释道网络电台八天开发全程 项目发起 功能和界面初步设定 在Android Studio中完成界面设计 实 ...

  10. APP开发流程实例讲解-儒释道网络电台八天开发全程-实现功能代码:播放控制

    APP开发流程实例讲解-儒释道网络电台八天开发全程 实现功能代码:播放控制 APP开发流程实例讲解-儒释道网络电台八天开发全程 项目发起 功能和界面初步设定 在Android Studio中完成界面设 ...

最新文章

  1. 密钥生成并配置_基于密钥的SSH认证流程
  2. JScrollPane 滚动处理
  3. idea自动更新java_IntelliJ IDEA自动更新资源文件
  4. Oracle数据库用户失效对象,Oracle数据库对象失效解决
  5. python中threading模块详解及常用方法_Python常用模块功能简介(二)threading
  6. 我们要在离职时,优雅地说再见!
  7. 190328文件处理
  8. linux下常用文件传输命令(转)
  9. apktook 反编译错误
  10. 用Python绘制了若干张词云图,惊艳了所有人
  11. Floyd最短路算法
  12. 机器学习笔记(九)——数据降维:主成分分析法(PCA)
  13. 黑客帝国角色 之 先知的另类解读
  14. cents7配置gradle4
  15. vtk读取CT序列mip投影
  16. Android Studio连Bmob的巨坑
  17. B0505S-1WR3 隔离模块DC/DC
  18. CityMaker 8二次开发记事
  19. 曙光服务器虚拟软驱,曙光IPMI系统管理平台用户使用指南(一).pdf
  20. iperf java_网络性能测试工具iperf详解

热门文章

  1. 向数据库插入数据时出现乱码 --设置连接数据库的编码
  2. JAVA怎么出现10行10列的星号_C语言输出n行n列星号
  3. 6.3交换器(Exchangers)
  4. Hibernate的单向N-1关联(一)
  5. 【渝粤教育】国家开放大学2018年秋季 1018t国际公法 参考试题
  6. 用信号量及其PV操作处理实际问题
  7. flask + apidoc 生成接口文档(附加一个坑)
  8. 编程语言-初学者常见的几个问题
  9. Uiautomator之入门
  10. [转] 丢了自己,要记得找回了~~我们80后的那些忧伤