IOS中對圖片進行重繪處理的方法總結(jié)
一调煎、CGImageRef是什么
CGImageRef是定義在QuartzCore框架中的一個結(jié)構(gòu)體指針缴啡,用C語言編寫移袍。在CGImage.h文件中,我們可以看到下面的定義:
typedef struct CGImage *CGImageRef;
CGImageRef 和 struct CGImage * 是完全等價的线椰。這個結(jié)構(gòu)用來創(chuàng)建像素位圖岗钩,可以通過操作存儲的像素位來編輯圖片纽窟。
QuartzCore這個框架是可移植的。
二兼吓、CGImageRef相關(guān)的一些方法解析
CGImageRef __nullable 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)
通過這個方法臂港,我們可以創(chuàng)建出一個CGImageRef類型的對象,下面分別對參數(shù)進行解釋:
sizt_t是定義的一個可移植性的單位视搏,在64位機器中為8字節(jié)审孽,32位位4字節(jié)。
width:圖片寬度像素
height:圖片高度像素
bitsPerComponent:每個顏色的比特數(shù)浑娜,例如在rgba-32模式下為8
bitsPerPixel:每個像素的總比特數(shù)
bytesPerRow:每一行占用的字節(jié)數(shù)佑力,注意這里的單位是字節(jié)
space:顏色空間模式,例如const CFStringRef kCGColorSpaceGenericRGB 這個函數(shù)可以返回一個顏色空間對象棚愤。
bitmapInfo:位圖像素布局搓萧,枚舉如下:
typedef CF_OPTIONS(uint32_t, CGBitmapInfo) {
kCGBitmapAlphaInfoMask = 0x1F,
kCGBitmapFloatComponents = (1 << 8),
kCGBitmapByteOrderMask = 0x7000,
kCGBitmapByteOrderDefault = (0 << 12),
kCGBitmapByteOrder16Little = (1 << 12),
kCGBitmapByteOrder32Little = (2 << 12),
kCGBitmapByteOrder16Big = (3 << 12),
kCGBitmapByteOrder32Big = (4 << 12)
}
provider:數(shù)據(jù)源提供者
decode[]:解碼渲染數(shù)組
shouldInterpolate:是否抗鋸齒
intent:圖片相關(guān)參數(shù)
使用實例
+ (UIImage *)mosaicImage:(UIImage *)image withLevel:(int)level
{
unsigned char *imgPixel = RequestImagePixelData(image);
CGImageRef inImageRef = [image CGImage];
GLuint width = CGImageGetWidth(inImageRef);
GLuint height = CGImageGetHeight(inImageRef);
unsigned char prev[4] = {0};
int bytewidth = width*4;
int i,j;
int val = level;
for(i=0;i<height;i++) {
if (((i+1)%val) == 0) {
memcpy(imgPixel+bytewidth*i, imgPixel+bytewidth*(i-1), bytewidth);
continue;
}
for(j=0;j<width;j++) {
if (((j+1)%val) == 1) {
memcpy(prev, imgPixel+bytewidth*i+j*4, 4);
continue;
}
memcpy(imgPixel+bytewidth*i+j*4, prev, 4);
}
}
NSInteger dataLength = width*height* 4;
//下面的代碼創(chuàng)建要輸出的圖像的相關(guān)參數(shù)
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imgPixel, dataLength, NULL);
// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
//創(chuàng)建要輸出的圖像
CGImageRef imageRef = CGImageCreate(width, height,
bitsPerComponent,
bitsPerPixel,
bytewidth,
colorSpaceRef,
bitmapInfo,
provider,
NULL, NO, renderingIntent);
UIImage *mosaicImage = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation];
CFRelease(imageRef);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
return mosaicImage;
}
CGImageRef CGImageMaskCreate(size_t width, size_t height,
size_t bitsPerComponent, size_t bitsPerPixel, size_t bytesPerRow,
CGDataProviderRef provider, const CGFloat decode[], bool shouldInterpolate)
這個方法用于創(chuàng)建mask圖片圖層,可以設(shè)置其顯示部分與不顯示部分達到特殊的效果宛畦,參數(shù)意義同上。
/* Return a copy of `image'. Only the image structure itself is copied; the
underlying data is not. */
CG_EXTERN CGImageRef __nullable CGImageCreateCopy(CGImageRef cg_nullable image)
CG_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
這個方法可以復(fù)制一個CGImageRef對象
/* Create an image from `source', a data provider of JPEG-encoded data. */
CG_EXTERN CGImageRef __nullable CGImageCreateWithJPEGDataProvider(
CGDataProviderRef cg_nullable source, const CGFloat * __nullable decode,
bool shouldInterpolate,
CGColorRenderingIntent intent)
CG_AVAILABLE_STARTING(__MAC_10_1, __IPHONE_2_0);
通過JPEG數(shù)據(jù)源獲取圖像
/* Create an image using `source', a data provider for PNG-encoded data. */
CG_EXTERN CGImageRef __nullable CGImageCreateWithPNGDataProvider(
CGDataProviderRef cg_nullable source, const CGFloat * __nullable decode,
bool shouldInterpolate,
CGColorRenderingIntent intent)
CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
通過PNG數(shù)據(jù)源獲取圖像
GImageRef CGImageCreateWithImageInRect(CGImageRef image,
CGRect rect)
截取圖像的一個區(qū)域重繪圖像
CGImageRef CGImageCreateWithMask(CGImageRef image, CGImageRef mask)
截取mask圖像的某一區(qū)域重繪
CGImageRef CGImageCreateWithMaskingColors(CGImageRef image,
const CGFloat components[])
通過顏色分量數(shù)組創(chuàng)建位圖
CGImageRef CGImageCreateCopyWithColorSpace(CGImageRef image,
CGColorSpaceRef space)
通過顏色空間模式復(fù)制位圖
CGImageRef CGImageRetain(CGImageRef image)
引用+1
void CGImageRelease(CGImageRef image)
引用-1
bool CGImageIsMask(CGImageRef image)
返回是否為Mask圖層
size_t CGImageGetWidth(CGImageRef image)
獲取寬度像素
size_t CGImageGetHeight(CGImageRef image)
獲取高度像素
下面這些方法分別獲取相應(yīng)屬性
size_t CGImageGetBitsPerComponent(CGImageRef image)
size_t CGImageGetBitsPerPixel(CGImageRef image)
size_t CGImageGetBytesPerRow(CGImageRef image)
CGColorSpaceRef CGImageGetColorSpace(CGImageRef image)CG_EXTERN CGImageAlphaInfo CGImageGetAlphaInfo(CGImageRef image)
CGDataProviderRef CGImageGetDataProvider(CGImageRef image)
const CGFloat *CGImageGetDecode(CGImageRef image)
bool CGImageGetShouldInterpolate(CGImageRef image)
CGColorRenderingIntent CGImageGetRenderingIntent(CGImageRef image)
CGBitmapInfo CGImageGetBitmapInfo(CGImageRef image)
應(yīng)用舉例
UIImage *imageFrom = [UIImage imageWithContentsOfFile:imagePath1];
size_t width = CGImageGetWidth(imageFrom.CGImage);//寬32 點數(shù)
size_t height = CGImageGetHeight(imageFrom.CGImage);//高32
size_t bpp = CGImageGetBitsPerPixel(imageFrom.CGImage);//每個像素32bits
size_t bpc = CGImageGetBitsPerComponent(imageFrom.CGImage);//8 每個像素幾位
size_t bpr= CGImageGetBytesPerRow(imageFrom.CGImage);//每行128字節(jié)=32*4字節(jié)
CGColorSpaceRef spaceRef = CGImageGetColorSpace(imageFrom.CGImage);
CGBitmapInfo mapInfo =CGImageGetBitmapInfo(imageFrom.CGImage);
CGImageAlphaInfo alphaInfo =CGImageGetAlphaInfo(imageFrom.CGImage);
CFStringRef type =CGImageGetUTType(imageFrom.CGImage);//public.png
CFDataRef rawData = CGDataProviderCopyData(CGImageGetDataProvider(imageFrom.CGImage));//獲取圖片原始像素數(shù)據(jù)
void *data =(void*) CFDataGetBytePtr(rawData);oc