iOS圖片處理,截圖,縮放,存儲

ngmm 10年前發布 | 4K 次閱讀 Objective-C IOS
圖片的處理大概分 截圖(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的文件里
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);
得到圖像數據區后就可以很方便的實現圖像處理的算法。
 本文由用戶 ngmm 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!