圖片的處理大概分 截圖(capture), ?縮放(scale), 設(shè)定大小(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的文件里和儲存到手機(jī)的圖片庫里
1)?儲存到app的文件里
NSString *path = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:pathatomically:YES];
把要處理的圖片, 以image.png名稱存到app home下的Documents目錄里
2)儲存到手機(jī)的圖片庫里(必須在真機(jī)使用锦援,模擬器無法使用)
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封裝了變換的參數(shù)灵寺,使得在不同的坐標(biāo)系里操作圖像非常方便曼库。缺點就是,獲取圖像的數(shù)據(jù)不是那么方便略板。下面會給出獲取數(shù)據(jù)區(qū)的代碼毁枯。
1. 從UIView中獲取圖像相當(dāng)于窗口截屏。
(ios提供全局的全屏截屏函數(shù)UIGetScreenView(). 如果需要特定區(qū)域的圖像蚯根,可以crop一下)
CGImageRef screen = UIGetScreenImage();
UIImage* image = [UIImage imageWithCGImage:screen];
2. 對于特定UIView的截屏后众。
(可以把當(dāng)前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. 如果需要裁剪指定區(qū)域。
(可以path & clip教藻,以下例子是建一個200x200的圖像上下文距帅,再截取出左上角)
UIGraphicsBeginImageContext(CGMakeSize(200,200));
CGContextRefcontext=UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
// ...把圖寫到context中,省略[indent]CGContextBeginPath();
CGContextAddRect(CGMakeRect(0,0,100,100));
CGContextClosePath();[/indent]CGContextDrawPath();
CGContextFlush();// 強(qiáng)制執(zhí)行上面定義的操作
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.互相轉(zhuǎn)換UImage和CGImage绍移。
(UImage封裝了CGImage, 互相轉(zhuǎn)換很容易)
UIImage* imUI=nil;
CGImageRef imCG=nil;
imUI = [UIImage initWithCGImage:imCG];
imCG = imUI.CGImage;
6. 從CGImage上獲取圖像數(shù)據(jù)區(qū)。
(在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.顯示圖像數(shù)據(jù)區(qū)讥电。
(顯示圖像數(shù)據(jù)區(qū)蹂窖,也就是unsigned char*轉(zhuǎn)為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);
得到圖像數(shù)據(jù)區(qū)后就可以很方便的實現(xiàn)圖像處理的算法。
轉(zhuǎn)自別處恩敌,用于自己筆記之用瞬测。侵刪