先上效果圖
添加方法
+ (UIImage*)mosaicWithImage:(UIImage *)image
獲取圖片的寬和高
CGImageRef是定義在QuartzCore框架中的一個(gè)結(jié)構(gòu)體指針嗓节,用C語(yǔ)言編寫CGImageRef 和 struct CGImage * 是完全等價(jià)的。這個(gè)結(jié)構(gòu)用來創(chuàng)建像素位圖,可以通過操作存儲(chǔ)的像素位來編輯圖片。
CGImageRef imageRef = image.CGImage;
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
動(dòng)態(tài)獲取顏色空間
CGColorSpaceRef colorSpaceRef = CGImageGetColorSpace(imageRef);
創(chuàng)建位圖上下文(解析圖片信息履澳,繪制圖片)
開辟一塊內(nèi)存空間草描,用于處理馬賽克圖片
參數(shù)一 數(shù)據(jù)源
參數(shù)二 圖片寬
參數(shù)三 圖片高
參數(shù)四 表示每一個(gè)像素點(diǎn)别瞭,每個(gè)分量大卸恪(每個(gè)8位)像素點(diǎn)由ARGB組成
參數(shù)五 每一行的大小 = 寬度*4
參數(shù)六 顏色空間
參數(shù)七 位圖信息(是否需要透明度)
指定bitmap是否包含alpha通道只冻,像素中alpha通道的相對(duì)位置,像素組件是整形還是浮點(diǎn)型等信息的字符串
當(dāng)你調(diào)用這個(gè)函數(shù)的時(shí)候犯犁,Quartz創(chuàng)建一個(gè)位圖繪制環(huán)境属愤,也就是位圖上下文女器。當(dāng)你向上下文中繪制信息時(shí)酸役,Quartz把你要繪制的信息作為位圖數(shù)據(jù)繪制到指定的內(nèi)存塊。一個(gè)新的位圖上下文的像素格式由三個(gè)參數(shù)決定:每個(gè)組件的位數(shù)驾胆,顏色空間涣澡,alpha選項(xiàng)。alpha值決定了繪制像素的透明性
CGContextRef contextRef = CGBitmapContextCreate(nil, width, height, 8, width*4, colorSpaceRef, kCGImageAlphaPremultipliedLast);
根據(jù)圖片上下文丧诺,繪制顏色空間的圖片
CGContextDrawImage(contextRef, CGRectMake(0, 0, width, height), imageRef);
獲取位圖像素?cái)?shù)組
unsigned char * bitmapData = CGBitmapContextGetData(contextRef);
位圖加馬賽克,循環(huán)遍歷每一個(gè)像素點(diǎn)
NSUInteger level = 10;
NSUInteger currentIndex,preCurrentIndex;
unsigned char* pixels[4] = {0};
for (NSUInteger i = 0; i < height - 1; i++){
for(NSUInteger j = 0; j < width - 1; j++){
currentIndex = (i * width) + j;
if(i%level == 0){
if(j%level == 0){
memcpy(pixels, bitmapData + 4 *currentIndex, 4);
}else{
memcpy(bitmapData + 4 * currentIndex, pixels, 4);
}
}else{
preCurrentIndex = (i-1) * width + j;
memcpy(bitmapData + 4 * currentIndex, bitmapData + 4 *preCurrentIndex, 4);
}
}
}
獲取位圖數(shù)據(jù)集合
NSUInteger size = width * height * 4;
CGDataProviderRef providerRef = CGDataProviderCreateWithData(NULL, bitmapData, size, NULL);
創(chuàng)建馬賽克位圖
參數(shù)一 圖片寬
參數(shù)二 圖片高
參數(shù)三 每個(gè)像素點(diǎn)入桂,每一個(gè)分量的大小
參數(shù)四 每一個(gè)像素點(diǎn)的大小
參數(shù)五 每一行的內(nèi)存大小
參數(shù)六 顏色空間
參數(shù)七 位圖信息
參數(shù)八 數(shù)據(jù)源
參數(shù)九 數(shù)據(jù)解碼器
參數(shù)十 是否抗鋸齒
參數(shù)十一 渲染器
通過這個(gè)方法,我們可以創(chuàng)建出一個(gè)CGImageRef類型的對(duì)象
CGImageRef mosaicImageRef = CGImageCreate(width, height, 8, 4*8, width*4, colorSpaceRef, kCGImageAlphaPremultipliedLast, providerRef, NULL, NO, kCGRenderingIntentDefault);
創(chuàng)建馬賽克位圖上下文驳阎,填充顏色
CGContextRef mosaicContextRef = CGBitmapContextCreate(nil, width, height, 8, width*4, colorSpaceRef, kCGImageAlphaPremultipliedLast);
繪制圖片
CGContextDrawImage(mosaicContextRef, CGRectMake(0, 0, width, height), mosaicImageRef);
CGImageRef resultImageRef = CGBitmapContextCreateImage(mosaicContextRef);
UIImage *mosaicImage = [UIImage imageWithCGImage:resultImageRef];
釋放內(nèi)存
CGImageRelease(resultImageRef);
CGImageRelease(mosaicImageRef);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(providerRef);
CGContextRelease(contextRef);
CGContextRelease(mosaicContextRef);
返回馬賽克圖片
return mosaicImage;