一 CGImageRef和UIImage的互相轉(zhuǎn)化
CGImageRef 是一個(gè)結(jié)構(gòu)體指針脑融,通過(guò)CGImageRef可以獲得UIImage的所有參數(shù)展懈,比如像素寬高绳泉、顏色通道位深冠跷、像素點(diǎn)位深合是、像素點(diǎn)字節(jié)排列及讀取順序等信息衫樊,CGImageRef與UIImage的互相轉(zhuǎn)化如下:
UIImage 轉(zhuǎn) CGImageRef
UIImage *image = [UIImage imageNamed:@"testImg"];
CGImageRef imageRef = image.CGImage;
CGImageRef 轉(zhuǎn) UIImage
CGImageRef imageRef = [UIImage imageNamed:@"testImg"].CGImage;
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
切記每次使用CGImageRef一定要記得釋放飒赃,否則會(huì)造成內(nèi)存泄露,釋放方式也很簡(jiǎn)單科侈,如下所示:
CGImageRelease(imageRef);
二 CGImageRef的創(chuàng)建方法
使用png數(shù)據(jù)源創(chuàng)建
使用方法
CGImageCreateWithPNGDataProvider(CGDataProviderRef cg_nullable source, const CGFloat * __nullable decode, bool shouldInterpolate, CGColorRenderingIntent intent)
使用示例:
NSString *path = [[NSBundle mainBundle] pathForResource:@"bundle8Bit" ofType:@".png"];
NSData *bundleImgData = [NSData dataWithContentsOfFile:path];
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)bundleImgData);
CGImageRef imageRef = CGImageCreateWithPNGDataProvider(provider, NULL, true, kCGRenderingIntentDefault);
UIImage *image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
self.imageV.image = image;
使用jpeg數(shù)據(jù)源創(chuàng)建
使用方法
CGImageCreateWithJPEGDataProvider(CGDataProviderRef cg_nullable source, const CGFloat * __nullable decode, bool shouldInterpolate, CGColorRenderingIntent intent)
使用示例:
NSString *path = [[NSBundle mainBundle] pathForResource:@"houst" ofType:@".jpg"];
NSData *bundleImgData = [NSData dataWithContentsOfFile:path];
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)bundleImgData);
CGImageRef imageRef = CGImageCreateWithJPEGDataProvider(provider, NULL, true, kCGRenderingIntentDefault);
UIImage *image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
self.imageV.image = image;
使用矩陣數(shù)據(jù)源創(chuàng)建
使用矩陣數(shù)據(jù)源創(chuàng)建圖片是最靈活的一種方法载佳,基本上可以完全滿足我們所有的定制化需求,可以在實(shí)現(xiàn)對(duì)圖片進(jìn)行像素級(jí)的改動(dòng)臀栈,使用方法名
CGImageCreate(size_t width, size_t height,size_t bitsPerComponent, size_t bitsPerPixel, size_t bytesPerRow, CGColorSpaceRef cg_nullable space, CGBitmapInfo bitmapInfo,CGDataProviderRef cg_nullable provider,const CGFloat * __nullable decode, bool shouldInterpolate,CGColorRenderingIntent intent)
參數(shù)解析如下:
size_t width 圖片寬度(像素級(jí)別)
size_t height 圖片高度(像素級(jí)別)
size_t bitsPerComponent 每個(gè)像素通道位深(bit數(shù)量)
size_t bitsPerPixel 每個(gè)像素點(diǎn)位深(bit數(shù)量)
size_t bytesPerRow 每行像素空間大懈沼(單位 字節(jié))
CGColorSpaceRef cg_nullable space 顏色空間
CGBitmapInfo bitmapInfo 像素排列及讀取順序
CGDataProviderRef cg_nullable provider 數(shù)據(jù)提供源
const CGFloat * __nullable decode 解碼arrr 一般傳入null 保持源數(shù)據(jù)
bool shouldInterpolate 是否使用差值來(lái)平滑過(guò)渡圖像
CGColorRenderingIntent intent 從一個(gè)顏色空間map到另一個(gè)顏色空間的方式
使用方式
//創(chuàng)建一個(gè)寬2000像素 高3000像素 每通道8bit 一共三通道(無(wú)alpha通道) 使用rgb顏色空間 的一張純紅色的圖片
size_t imageWidth = 2000;
size_t imageHeight = 3000;
size_t bitsPerComponent = 8;
size_t bitsPerPixel = 24;
size_t bytesPerRow = imageWidth * bitsPerPixel / 8;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGImageAlphaNone | kCGImageByteOrderDefault;
//創(chuàng)建純紅色數(shù)據(jù)源
int byteIndex = 0;
unsigned char *rawData = (unsigned char*) malloc(imageWidth * imageHeight * 3);
for (int i = 0; i < imageHeight; i ++) {
for (int j = 0; j < imageWidth; j ++) {
rawData[byteIndex ++] = 255; //red通道
rawData[byteIndex ++] = 0; //green通道
rawData[byteIndex ++] = 0; //blue通道
}
}
CGDataProviderRef dataProviderRef = CGDataProviderCreateWithData(NULL,
rawData,
imageWidth*imageHeight*3,
NULL);
CGImageRef imageRef = CGImageCreate(imageWidth,
imageHeight,
bitsPerComponent,
bitsPerPixel,
bytesPerRow,
colorSpaceRef,
bitmapInfo,
dataProviderRef,
nil,
NO,
kCGRenderingIntentDefault);
UIImage *image = [UIImage imageWithCGImage:imageRef];
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(dataProviderRef);
CGImageRelease(imageRef);
self.imageV.image = image;
創(chuàng)建結(jié)果
size_t bitsPerComponentCheck = CGImageGetBitsPerComponent(imageRef);
printf("每個(gè)通道占用的位數(shù):%zu\n",bitsPerComponentCheck);
size_t bitsPerPixelCheck = CGImageGetBitsPerPixel(imageRef);
printf("每個(gè)像素占用的位數(shù):%zu",bitsPerPixelCheck);
輸出結(jié)果:
每個(gè)通道占用的位數(shù):8
每個(gè)像素占用的位數(shù):24
跟我們創(chuàng)建的參數(shù)一致
三 使用CGImageRef對(duì)圖片進(jìn)行裁剪
方法 CGImageCreateWithImageInRect(CGImageRef cg_nullable image, CGRect rect)
使用方法幾示例如下
NSString *path = [[NSBundle mainBundle] pathForResource:@"bundle8Bit" ofType:@".png"];
NSData *bundleImgData = [NSData dataWithContentsOfFile:path];
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)bundleImgData);
CGImageRef imageRef = CGImageCreateWithPNGDataProvider(provider, NULL, true, kCGRenderingIntentDefault);
CGImageRef clipImageRef = CGImageCreateWithImageInRect(imageRef, CGRectMake(0, 0, 100, 200));
UIImage *image = [UIImage imageWithCGImage:clipImageRef];
self.imageV.image = image;
以圖片左上角為頂點(diǎn),裁剪出來(lái)像素寬100挂脑,高200的圖片區(qū)域
裁剪原圖:
裁剪出來(lái)的圖片: