UIImage 图片处理:截图,缩放,设定大小,存储

简介: <p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"> <span style="font-family:mceinline"><span style="font-size:18px">1.等比率缩放</span><br> - (UIImage *)scaleImage:(

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封装了变换的参数,使得在不同的坐标系里操作图像非常方便。缺点就是,获取图像的数据不是那么方便。下面会给出获取数据区的代码。

 

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

(ios提供全局的全屏截屏函数UIGetScreenView(). 如果需要特定区域的图像,可以crop一下)

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

2. 对于特定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. }

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

(可以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();

4. 存储图像。

(分别存储到home目录文件和图片库文件。)

存储到目录文件是这样

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

若要存储到图片库里面

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


5.  互相转换UImage和CGImage。

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

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

6. 从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. m_PixelBuf[index + 0] = 255 - m_PixelBuf[index + 0];// b
  19. m_PixelBuf[index + 1] = 255 - m_PixelBuf[index + 1];// g
  20. m_PixelBuf[index + 2] = 255 - m_PixelBuf[index + 2];// r
  21. }

  22. ctx = CGBitmapContextCreate(m_PixelBuf, width, height, bpb, bpl, CGImageGetColorSpace( inImage ), kCGImageAlphaPremultipliedFirst );
  23. CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
  24. UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
  25. CGContextRelease(ctx);
  26. return rawImage;
  27. }

 

7. 显示图像数据区。

(显示图像数据区,也就是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);
得到图像数据区后就可以很方便的实现图像处理的算法。

完,欢迎指正!

目录
相关文章
|
4月前
|
iOS开发 Perl
UITableView的单元格加载通过SDWebImage下载的超大尺寸图片崩溃问题及解决方案
UITableView的单元格加载通过SDWebImage下载的超大尺寸图片崩溃问题及解决方案
42 2
|
2月前
|
PHP 数据安全/隐私保护 计算机视觉
ThinkPHP图片处理之压缩图片大小,图片处理之图片水印(添加平铺文字水印,并设置文字之间的间距和文字的角度)
ThinkPHP图片处理之压缩图片大小,图片处理之图片水印(添加平铺文字水印,并设置文字之间的间距和文字的角度)
39 1
|
3月前
|
存储 编解码 API
如何通过编程获取桌面分辨率、操作像素点颜色、保存位图和JPG格式图片,以及图片数据的处理和存储方式
如何通过编程获取桌面分辨率、操作像素点颜色、保存位图和JPG格式图片,以及图片数据的处理和存储方式
56 0
|
移动开发 前端开发 JavaScript
【移动端】实现相册的上传和缩放裁剪
做项目时,在移动端,需要实现用户相册图片的上传,并对图片进行缩放裁剪的功能。下面说一下实现流程。
196 1
【移动端】实现相册的上传和缩放裁剪
|
小程序 容器
【微信小程序】image组件的4种缩放模式与9种裁剪模式
假设有一个容器(这个容器的宽高就是设置的样式),要将图片放进去。而aspectFit的特点就是保持图片不变形,且容器要“刚好”将这个图片装进去。如果原始图片比容器大,就要被等比例缩小;如果原始图片比容器小,就会被等比例放大。一直放大或缩小到图片的某一条边刚好和容器的一条边重合。
1268 0
【图片操作】给图片添加滤镜
现在我们都喜欢给图片添加滤镜,现在很多相机也自带了许多滤镜。我们可以在拍照的时候选择需要的滤镜。但是有时候我们需要给大量图片添加同样的滤镜,这个时候手动添加就非常麻烦了。为了方便,我们可以使用程序来帮我们完成添加滤镜的操作。
237 0
【图片操作】混合图片
混合图片的方式有很多种,我们先来看看如何通过混合图片的通道来实现图片混合。
146 0
|
Python
【图片操作】批量生成缩略图
在我们日常生活中,缩略图很大程度减少了我们内存的使用。如果我们看一张图片就必须加载完成后才能看,那么我们就会发现很多应用都变慢了很多,而且流量也消耗的很快。今天我们就来看看Python生成缩略图的操作。
357 0
|
JavaScript 前端开发 API
使用copper实现图片在线裁剪
2017年写的文章,搬运一下,用cropper实现图片的裁剪。
2720 3
|
C# 图形学 索引
上传图片时,使用GDI+中重绘方式将CMYK图片转为RGB图片
原文:上传图片时,使用GDI+中重绘方式将CMYK图片转为RGB图片 我们知道,如果网站上传图片时,如果用户上传的是CMYK图片,那么在网站上将是无法显示的,通常的现象是出现一个红叉。
1203 0