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

图片的处理大概就分 截图(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. 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. }

 

得到圖像數據區後就可以很方便的實現圖像處理的算法。下麵給顯示圖像數據區的方法,也就是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);

關於圖像獲取方麵,在這裏應該都覆蓋到了,不正之處,歡迎指正。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
UIImageiOS中的一个类,用于表示图像或照片。你可以使用UIImage来加载、显示和处理图像。下面是一些使用UIImage的常见操作: 1. 加载图像: 你可以使用UIImage(named:)方法从应用程序的资源中加载图像文件,例如: ``` let image = UIImage(named: "imageName") ``` 也可以使用UIImage(contentsOfFile:)方法从应用程序的沙盒中加载图像文件。 2. 显示图像: 你可以将UIImage对象分配给UIImageView的image属性,然后将UIImageView添加到视图层次结构中以显示图像,例如: ``` let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) imageView.image = image view.addSubview(imageView) ``` 3. 调整图像大小: 你可以使用UIImage的resizableImage(withCapInsets:resizingMode:)方法来调整图像的大小。这个方法会返回一个可调整大小的新图像,例如: ``` let resizedImage = image.resizableImage(withCapInsets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10), resizingMode: .stretch) ``` 4. 图像处理UIImage还提供了一些方法来进行图像处理,例如: - 使用UIImageJPEGRepresentation(_:compressionQuality:)方法将图像转换为JPEG格式的数据。 - 使用UIImagePNGRepresentation(_:)方法将图像转换为PNG格式的数据。 - 使用draw(in:)方法在指定的矩形中绘制图像。 以上是UIImage的一些常见用法,希望对你有帮助。如果你有具体的问题或需求,可以提供更多详细的信息,我会尽力帮助你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值