UIImage图片的处理大概分 截图(capture), 缩放(scale), 设定大小(resize), 存储(save)

1.等比率缩放

- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize{
UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize);
[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();return scaledImage;}

2.自定长宽

- (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
{
UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
[image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();return reSizeImage;}

3.处理某个特定View

只要是继承UIView的object 都可以处理
必须先import QuzrtzCore.framework


-(UIImage*)captureView:(UIView *)theView{
CGRect rect = theView.frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();return img;}

4.储存图片

储存图片这里分成储存到app的文件里和储存到手机的图片库里

1) 储存到app的文件里

把要处理的图片, 以image.png名称存到app home下的Documents目录里
NSString *path = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:pathatomically:YES];

2)储存到手机的图片库里(必须在真机使用,模拟器无法使用)

CGImageRef screen = UIGetScreenImage();
UIImage* image = [UIImage imageWithCGImage:screen];
CGImageRelease(screen);
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
UIGetScreenImage(); // 原来是private(私有)api, 用来截取整个画面,不过SDK 4.0后apple就开放了

//====================================================================================

以下代码用到了Quartz Framework 和 Core Graphics Framework. 在workspace的framework目录里添加这两个framework.在UIKit里,图像类UIImage和CGImageRef的画图操作都是通过Graphics Context来完成。Graphics Context封装了变换的参数,使得在不同的坐标系里操作图像非常方便。缺点就是,获取图像的数据不是那么方便。下面会给出获取数据区的代码。

1. 从UIView中获取图像相当于窗口截屏

(ios提供全局的全屏截屏函数UIGetScreenView(). 如果需要特定区域的图像,可以crop一下)
CGImageRef screen = UIGetScreenImage();
UIImage* image = [UIImage imageWithCGImage:screen];

2. 对于特定UIView的截屏。

(可以把当前View的layer,输出到一个ImageContext中,然后利用这个ImageContext得到UIImage)-(UIImage*)captureView: (UIView *)theView
{
CGRect rect = theView.frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context =UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();return img;
}

3. 如果需要裁剪指定区域。

(可以path & clip,以下例子是建一个200x200的图像上下文,再截取出左上角)
UIGraphicsBeginImageContext(CGMakeSize(200,200));
CGContextRefcontext=UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
// ...把图写到context中,省略[indent]CGContextBeginPath();
CGContextAddRect(CGMakeRect(0,0,100,100));
CGContextClosePath();[/indent]CGContextDrawPath();
CGContextFlush(); // 强制执行上面定义的操作
UIImage* image = UIGraphicGetImageFromCurrentImageContext();
UIGraphicsPopContext();

4. 存储图像。

(分别存储到home目录文件和图片库文件。)
存储到目录文件是这样
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
若要存储到图片库里面
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

5. 互相转换UImage和CGImage。

(UImage封装了CGImage, 互相转换很容易)UIImage* imUI=nil;
CGImageRef imCG=nil;
imUI = [UIImage initWithCGImage:imCG];
imCG = imUI.CGImage;
6. 从CGImage上获取图像数据区。(在apple dev上有QA, 不过好像还不支持ios)下面给出一个在ios上反色的例子-(id)invertContrast:(UIImage*)img
{
CGImageRef inImage = img.CGImage;
CGContextRef ctx;
CFDataRef m_DataRef;
m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage)); int width = CGImageGetWidth( inImage );
int height = CGImageGetHeight( inImage );int bpc = CGImageGetBitsPerComponent(inImage);
int bpp = CGImageGetBitsPerPixel(inImage);
int bpl = CGImageGetBytesPerRow(inImage);UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
int length = CFDataGetLength(m_DataRef);NSLog(@"len %d", length);
NSLog(@"width=%d, height=%d", width, height);
NSLog(@"1=%d, 2=%d, 3=%d", bpc, bpp,bpl);for (int index = 0; index < length; index += 4)
{
m_PixelBuf[index + 0] = 255 - m_PixelBuf[index + 0];// b
m_PixelBuf[index + 1] = 255 - m_PixelBuf[index + 1];// g
m_PixelBuf[index + 2] = 255 - m_PixelBuf[index + 2];// r
}ctx = CGBitmapContextCreate(m_PixelBuf, width, height, bpb, bpl, CGImageGetColorSpace( inImage ), kCGImageAlphaPremultipliedFirst );
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
return rawImage;
}

7. 显示图像数据区.


(显示图像数据区,也就是unsigned char*转为graphics context或者UIImage或和CGImageRef)CGContextRef ctx = CGBitmapContextCreate(pixelBuf,width,height, bitsPerComponent,bypesPerLine, colorSpace,kCGImageAlphaPremultipliedLast );
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage* image = [UIImage imageWithCGImage:imageRef];
NSString* path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"ss.png"];
[UIImagePNGRepresentation(self.image) writeToFile:path atomically:YES];
CGContextRelease(ctx);
得到图像数据区后就可以很方便的实现图像处理的算法。

本文转载:http://blog.csdn.net/xuhuan_wh/article/details/6434055

iOS开发:UIImage 图片处理:截图,缩放,设定大小,存储相关推荐

  1. iOS修改UIImage图片尺寸大小逻辑分辨率

    iOS修改UIImage图片尺寸大小逻辑分辨率

  2. iOS开发中图片的一些处理操作(背景色,透明度,合成,大小)

    之前写过一篇有关更换图片背景色的文章,今天遇到一块儿了,就干脆重新整理一下了.iOS开发中常用的对图片的处理操作: 一.更换图片的背景颜色 /** * 改变图片背景为白色 * * @param ima ...

  3. ios开发 微博图片缩放处理错误_H5响应式开发必会之Viewport(视窗)详解

    什么是 Viewport?viewport 是用户网页的可视区域. viewport 翻译为中文可以叫做"视区". 手机浏览器是把页面放在一个虚拟的"窗口"(v ...

  4. 【ios开发】图片拉伸

    最近在做一个项目 其中要自己定制一个View 如图: 但是美工给了我的图片尺寸却是不一样的. 分别是599*80  26*61 于是就成了这样的效果. 很明显的发现取消四周不对劲. 于是我就去找美工姐 ...

  5. iOS开发-图片拉伸的实现

    如上图是一个按钮的背景图,在Android上,很多图片资源都是类似这样子的,但是由于按钮的高度及宽度与图片的世纪尺寸不同,所以需要采用9patch来实现拉伸处理, 可参考:http://www.cnb ...

  6. (0031) iOS 开发之图片压缩

    1. Aspect单词的, 都会按照图片的宽高比来拉伸.这样会显示不全照片 2. Scale单词的,都会对图片进行拉伸(缩放); 3. 没有出现Scale单词的,都不会对图片进行拉伸; UIViewC ...

  7. iOS开发之图片分辨率与像素对齐

    像素对齐的概念 在iOS中,有一个概念叫做像素对齐,如果像素不对齐,那么在GPU渲染时,需要进行插值计算,这个插值计算的过程会有性能损耗. 在模拟器上,有一个选项可以把像素不对齐的部分显示出来.  ...

  8. iOS开发-简单图片背景替换 实现抠图效果

    之前好奇, 想实现这样的功能   -----> iOS图像处理-(jpg去除白色背景) 把一张图片(.jpg)的白色背景抠掉,转成.png 格式的有alpha通道的透明图. 原图黑白分明, 像这 ...

  9. iOS开发-简单图片背景替换(实现抠图效果)

    之前好奇, 想实现这样的功能   -----> iOS图像处理-(jpg去除白色背景) 把一张图片(.jpg)的白色背景抠掉,转成.png 格式的有alpha通道的透明图. 原图黑白分明, 像这 ...

  10. IOS开发之——图片的内存优化

    一 概述 创建Image的方式有两种: UIImage *image=[UIImage imageNamed:imageName]-内存由系统管理 UIImage *image=[UIImage im ...

最新文章

  1. GCD的其他(不常用)方法
  2. php编写函数6,编写自己的PHP扩展函数
  3. php layui ajax多图上传,Laravel+Layer实现图片上传功能(整理篇)
  4. 继 承(面向对象特征之二)
  5. python解析excel公式_[python][openpyxl]读取excel中公式的结果值
  6. 1.安装flink-1.12.2
  7. TortoiseSVN中图标的含义
  8. [转载] python bytearray拼接_python-4-bytes和bytearray
  9. 运动目标跟踪(三)--搜索算法优化搜索方向之Meanshift
  10. python3.5安装pip_python详细安装pip教程
  11. Python量化交易学习笔记(39)——BaoStock股票数据下载
  12. winhex 使用教程
  13. mysql经典脚本_mysql常用脚本
  14. 数据库原理与应用第三版何玉洁第二章课后题答案
  15. 关于Ubuntu18.04+win10双系统开机引导错误的解决方法
  16. Jenkins与DevOps持续交付详解
  17. 2022.11.27一周总结
  18. redis开启rdb和aof后文件正确恢复
  19. 几个简单好用的APP分享给你
  20. 免费专利检索和下载网站(亲测超实用)

热门文章

  1. 利用python目录化整理PPT素材文件
  2. 文思海辉 墨尔本_团结墨尔本
  3. python机械编程入门先学什么_编程入门先学什么
  4. HTML5期末大作业:个人主页网站设计(6页)代码质量高 学生简单个人静态HTML网页设计作品 DIV布局个人介绍网页模板代码 DW学生个人网站制作成品下载
  5. JDK8 after时间日期api
  6. ElasticSearch近实时搜索的实现
  7. 自动更换壁纸的小软件:likecan
  8. xampp+phpstorm使用xdebug
  9. DOS(bat) 字符串替换原理
  10. hbw-utils - 基本数据类型的parse操作