图片的处理大概就分 截图(capture), 缩放(scale),设定大小(resize), 存储(save)
这几样比较好处理, 另外还有滤镜,擦试等, 以后再说
在这个Demo code裡, 我写了几个方法

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的文件里
NSString *path = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:pathatomically:YES];
這樣就把你要處理的圖片, 以image.png這個檔名存到app home底下的Documents目錄裡

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封裝了變換的參數,使得在不同的坐標係裏操作圖像非常方便。缺點就是,獲取圖像的數據不是那麼方便。下麵會給出獲取數據區的代碼。

從UIView中獲取圖像相當於窗口截屏。ios提供全局的全屏截屏函數UIGetScreenView(). 如果需要特定區域的圖像,可以crop一下。

  1. CGImageRef screen = UIGetScreenImage();
  2. UIImage* image = [UIImage imageWithCGImage:screen];

對於特定UIView的截屏,可以把當前View的layer,輸出到一個ImageContext中,然後利用這個ImageContext得到UIImage

  1. -(UIImage*)captureView: (UIView *)theView
  2. {
  3. CGRect rect = theView.frame;
  4. UIGraphicsBeginImageContext(rect.size);
  5. CGContextRef context =UIGraphicsGetCurrentContext();
  6. [theView.layer renderInContext:context];
  7. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  8. UIGraphicsEndImageContext();
  9. return img;
  10. }

如果需要裁剪製定區域,可以path & clip,以下例子是建一個200x200的圖像上下文,再截取出左上角

  1. UIGraphicsBeginImageContext(CGMakeSize(200,200));
  2. CGContextRefcontext=UIGraphicsGetCurrentContext();
  3. UIGraphicsPushContext(context);
  4. // ...把图写到context中,省略[indent]CGContextBeginPath();
  5. CGContextAddRect(CGMakeRect(0,0,100,100));
  6. CGContextClosePath();[/indent]CGContextDrawPath();
  7. CGContextFlush(); // 强制执行上面定义的操作
  8. UIImage* image = UIGraphicGetImageFromCurrentImageContext();
  9. UIGraphicsPopContext();

存储图像分为存储到home目录文件和图片库文件。存储到目录文件是这样

  1. NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"image.png"];
  2. [UIImagePNGRepresentation(image) writeToFile:path atomically:YES];

若要存储到图片库里面

  1. UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

UImage封装了CGImage, 互相转换很容易

  1. UIImage* imUI=nil;
  2. CGImageRef imCG=nil;
  3. imUI = [UIImage initWithCGImage:imCG];
  4. imCG = imUI.CGImage;

從CGImage上獲取圖像數據區,在apple dev上有QA, 不過好像還不支持ios
下麵給出一個在ios上反色的例子

  1. -(id)invertContrast:(UIImage*)img
  2. {
  3. CGImageRef inImage = img.CGImage;
  4. CGContextRef ctx;
  5. CFDataRef m_DataRef;
  6. m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
  7. int width = CGImageGetWidth( inImage );
  8. int height = CGImageGetHeight( inImage );
  9. int bpc = CGImageGetBitsPerComponent(inImage);
  10. int bpp = CGImageGetBitsPerPixel(inImage);
  11. int bpl = CGImageGetBytesPerRow(inImage);
  12. UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
  13. int length = CFDataGetLength(m_DataRef);
  14. NSLog(@"len %d", length);
  15. NSLog(@"width=%d, height=%d", width, height);
  16. NSLog(@"1=%d, 2=%d, 3=%d", bpc, bpp,bpl);
  17. for (int index = 0; index < length; index += 4)
  18. {
  19. m_PixelBuf[index + 0] = 255 - m_PixelBuf[index + 0];// b
  20. m_PixelBuf[index + 1] = 255 - m_PixelBuf[index + 1];// g
  21. m_PixelBuf[index + 2] = 255 - m_PixelBuf[index + 2];// r
  22. }
  23. ctx
    = CGBitmapContextCreate(m_PixelBuf, width, height, bpb, bpl,
    CGImageGetColorSpace( inImage ), kCGImageAlphaPremultipliedFirst );
  24. CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
  25. UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
  26. CGContextRelease(ctx);
  27. return rawImage;
  28. }

得到圖像數據區後就可以很方便的實現圖像處理的算法。下麵給顯示圖像數據區的方法,也就是unsigned char*轉為graphics context或者UIImage或和CGImageRef

    1. CGContextRef
      ctx = CGBitmapContextCreate(pixelBuf,width,height,
      bitsPerComponent,bypesPerLine, colorSpace,kCGImageAlphaPremultipliedLast
      );
    2. CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
    3. UIImage* image = [UIImage imageWithCGImage:imageRef];
    4. NSString* path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"ss.png"];
    5. [UIImagePNGRepresentation(self.image) writeToFile:path atomically:YES];
    6. CGContextRelease(ctx);

UIImage 图片处理:截图,缩放,设定大小,存储的更多相关文章

  1. 理解CSS3中的background-size(对响应性图片等比例缩放)

    理解CSS3中的background-size(对响应性图片等比例缩放) 阅读目录 background-size的基本属性 给图片设置固定的宽度和高度的 固定宽度400px和高度200px-使用ba ...

  2. CSS3中的background-size(对响应性图片等比例缩放)

    background-size的基本属性 background-size: 可以设定背景图像的尺寸,该属性是css3中的,在移动端使用的地方很多,比如最常见的地方在做响应性布局的时候,比如之前做的项目 ...

  3. css 如何实现图片等比例缩放

    在进行布局的时候,很多PM都要求图片等比例缩放,而且要求图片不失真,不变形,但是UI设计好了这个div的宽度又不能随意更改,而后台传过来的图片也不是等比例的图片,这就比较难受了,写成 width: 1 ...

  4. C#图片按比例缩放

    C#图片按比例缩放: // 按比例缩放图片 public Image ZoomPicture(Image SourceImage, int TargetWidth, int TargetHeight) ...

  5. 浅谈Android中拍照、从相册选择图片并截图相关知识点

    前言 我们在Android开发中经常会需要使用相机或者从相册中选取图片的情况,今天就把这里面相关的知识点总结下,方便以后开发的时候使用. 1.相机拍照并可自定义截图功能 我们先来看如何使用Intent ...

  6. 做了一个图片等比缩放的js

    做了一个图片等比缩放的js 芋头 发布在view:8447   今天改了一下博客的主题,发现博客主题在ie6下变样了,后来发现是因为某篇文章里的某个图片太大了撑开了容器,导致样式错位,前几天公司需求里 ...

  7. 背景图片等比缩放的写法background-size简写法

    1.背景图片或图标也可像img一样给其宽高就能指定其缩放大小了. 比如一个实际宽高36*28的图标,要缩小一半引用进来的写法就是: background:rgba(0, 0, 0, 0) url(&q ...

  8. CSS让DIV按照背景图片的比例缩放,并让背景图片填充整个元素(转)

    目的是:通过background的一系列属性,让DIV按照背景图片的比例缩放,并让背景图片填充整个DIV 首先我们需要让背景图片在指定的DIV中全部填充显示 之前看有用类似 background-at ...

  9. 如何安装nginx_lua_module模块,升级nginx,nginx-lua-fastdfs-GraphicsMagick动态生成缩略图,实现图片自动裁剪缩放

    如何安装nginx_lua_module模块,升级nginx,nginx-lua-fastdfs-GraphicsMagick动态生成缩略图,实现图片自动裁剪缩放 参考网站:nginx-lua-fas ...

随机推荐

  1. 10453 Make Palindrome (dp)

    Problem A Make Palindrome Input: standard input Output: standard output Time Limit: 8 seconds By def ...

  2. adb 异常报错----adb server is out of date. killing... ADB server didn't ACK * failed to start daemon *

    在Eclipse进行android开发的时候,由于要启动adb,但有时候其他的程序启动会占用adb程序的端口,这时候在对android程序进行调试的时候就会出现报错: 究其原因就是因为其他程序占用了a ...

  3. spark aggregate

    该函数官方的api,说的不是很明白: aggregate(zeroValue, seqOp, combOp) Aggregate the elements of each partition, and ...

  4. redhat enterprise 6.3 x86_64 上安装VirtualBox详细教程

    这个教程真难找..... 安装第一步遇到的问题就是不能使用yum安装包,这是由于redhat是收费版,所以需要更新yum源列表,具体可以参考 http://www.cnblogs.com/tina-s ...

  5. Signal ()函数详细介绍 Linux函数

    http://blog.csdn.net/ta893115871/article/details/7475095 Signal ()函数详细介绍 Linux函数 signal()函数理解 在<s ...

  6. Linux主机规划与磁盘分区

    各硬件设备在Linux中的文件名 在Linux系统当中,几乎所有的硬件设备文件都在/dev这个目录内. 各硬件设备在Linux中的文件名: 设备 设备在Linux中的文件名 IDE接口的硬盘 /dev ...

  7. JAVA程序猿面试题汇总

    (此话题将不断更新,请留意) 第一,谈谈final, finally, finalize的差别. final 用于声明属性,方法和类,分别表示属性不可变,方法不可覆盖,类不可继承.finally是异常 ...

  8. AspectJ教学

    这几天看JAVA基础看的有点头疼,决定时不时的换换口味,准备開始调研一些如今流行的技术,于是,開始埋头思考自己知识的盲区(当时,自己的知识盲区茫茫多...),想了几天后,决定要開始研究一下几种技术及实 ...

  9. Silverlight学习(四) domainservice动态多条件查询

    上次讲了silverlight+MVVN+EF的简单框架,能够实现简单的数据CURD,但是多条件动态的查询一直没有实现.在网上查阅了很多资料,发现自己走了很多误区,代码很难调试正确. 这次的查询是基于 ...

  10. 《C++ Primer Plus 6th》读书笔记 - 第九章 内存模型和名称空间

    1. 单独编译 1.1 头文件中常包含的内容: 函数原型 使用#define或const定义的符号常量 结构声明 类声明 模板声明 内联声明 1.2 只需将源代码文件加入到项目中,而不用加入头文件.这 ...