使用CGBitmapContextCreate創(chuàng)建繪制圖片的上下文
CGContextRef CGBitmapContextCreate (
void *data,
size_t width,
size_t height第步,
size_t bitsPerComponent收恢,
size_t bytesPerRow席覆,
CGColorSpaceRef colorspace橄浓,
CGBitmapInfo bitmapInfo
);
- 參數(shù)data指向繪圖操作被渲染的內存區(qū)域土童,這個內存區(qū)域大小應該為(bytesPerRow*height)個字節(jié)。如果對繪制操作被渲染的內存區(qū)域并無特別的要求工坊,那么可以傳遞NULL給參數(shù)date献汗。
- 參數(shù)width代表被渲染內存區(qū)域的寬度。
- 參數(shù)height代表被渲染內存區(qū)域的高度王污。
- 參數(shù)bitsPerComponent被渲染內存區(qū)域中組件在屏幕每個像素點上需要使用的bits位罢吃,舉例來說,如果使用32-bit像素和RGB顏色格式昭齐,那么RGBA顏色格式中每個組件在屏幕每個像素點上需要使用的bits位就為32/4=8尿招。
- 參數(shù)bytesPerRow代表被渲染內存區(qū)域中每行所使用的bytes位數(shù)。
- 參數(shù)colorspace用于被渲染內存區(qū)域的“位圖上下文”阱驾。
- 參數(shù)bitmapInfo指定被渲染內存區(qū)域的“視圖”是否包含一個alpha(透視)通道以及每個像素相應的位置就谜,除此之外還可以指定組件式是浮點值還是整數(shù)值。
而傳入?yún)?shù)呢信息又可以從位圖上下文中獲取
CG_EXTERN void * __nullable CGBitmapContextGetData(CGContextRef cg_nullable context)
CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
/* Return the width of the bitmap context `context', or 0 if `context' is
not a bitmap context. */
CG_EXTERN size_t CGBitmapContextGetWidth(CGContextRef cg_nullable context)
CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
/* Return the height of the bitmap context `context', or 0 if `context' is
not a bitmap context. */
CG_EXTERN size_t CGBitmapContextGetHeight(CGContextRef cg_nullable context)
CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
/* Return the bits per component of the bitmap context `context', or 0 if
`context' is not a bitmap context. */
CG_EXTERN size_t CGBitmapContextGetBitsPerComponent(CGContextRef cg_nullable context)
CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
/* Return the bits per pixel of the bitmap context `context', or 0 if
`context' is not a bitmap context. */
CG_EXTERN size_t CGBitmapContextGetBitsPerPixel(CGContextRef cg_nullable context)
CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
/* Return the bytes per row of the bitmap context `context', or 0 if
`context' is not a bitmap context. */
CG_EXTERN size_t CGBitmapContextGetBytesPerRow(CGContextRef cg_nullable context)
CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
/* Return the color space of the bitmap context `context', or NULL if
`context' is not a bitmap context. */
CG_EXTERN CGColorSpaceRef __nullable CGBitmapContextGetColorSpace(
CGContextRef cg_nullable context)
CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
/* Return the alpha info of the bitmap context `context', or
"kCGImageAlphaNone" if `context' is not a bitmap context. */
CG_EXTERN CGImageAlphaInfo CGBitmapContextGetAlphaInfo(
CGContextRef cg_nullable context)
CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
/* Return the bitmap info of the bitmap context `context', or 0 if `context'
is not a bitmap context. */
CG_EXTERN CGBitmapInfo CGBitmapContextGetBitmapInfo(
CGContextRef cg_nullable context)
CG_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
最終根據(jù)位圖上下文創(chuàng)建一個CGImageRef
CG_EXTERN CGImageRef __nullable CGBitmapContextCreateImage(
CGContextRef cg_nullable context)
CG_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
+ (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)建要輸出的圖像的相關參數(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;
}