#pragma mark - 马赛克
- (UIImage *)Mosaic:(UIImage *)inImage Level:(int)level
{
unsigned char *imgPixel = RequestImagePixelData(inImage);
CGImageRef inImageRef = [inImage 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;
//下面的代码创建要输出的图像的相关参数
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;
//创建要输出的图像
CGImageRef imageRef = CGImageCreate(width, height,
bitsPerComponent,
bitsPerPixel,
bytewidth,
colorSpaceRef,
bitmapInfo,
provider,
NULL, NO, renderingIntent);
UIImage *my_Image = [UIImage imageWithCGImage:imageRef];
CFRelease(imageRef);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
return my_Image;
}
网友评论