iOS圖片的偽裁剪(改變圖片的像素值)

0x00 原理

利用一張圖片事先畫好的圖片(以下稱為蒙板),蓋在要被裁剪的的圖片上,然后遍歷蒙板上的像素點(diǎn),修改被裁剪圖片對(duì)應(yīng)位置的像素的色值即可得到一些我們想要的不規(guī)則圖片了(比如人臉)

0x01 代碼實(shí)現(xiàn)(UIImage分類)

#define Mask8(x) ( (x) & 0xFF )
#define R(x) ( Mask8(x) )
#define G(x) ( Mask8(x >> 8 ) )
#define B(x) ( Mask8(x >> 16) )
#define A(x) ( Mask8(x >> 24) )
#define RGBAMake(r, g, b, a) ( Mask8(r) | Mask8(g) << 8 | Mask8(b) << 16 | Mask8(a) << 24 )
- (UIImage *)processWithMaskImage:(UIImage *)maskImg {
    if (self == nil) {
        return nil;
    }
    // 1. Get the raw pixels of the image
    UInt32 *inputPixels;
    UInt32 *maskPixels;
    
    CGImageRef maskCGImage = [maskImg CGImage]; //蒙板圖片
    CGImageRef inputCGImage = [self CGImage];//準(zhǔn)備被裁剪的圖片
    NSUInteger inputWidth = CGImageGetWidth(inputCGImage);
    NSUInteger inputHeight = CGImageGetHeight(inputCGImage);
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    NSUInteger bytesPerPixel = 4;
    NSUInteger bitsPerComponent = 8;
    
    NSUInteger inputBytesPerRow = bytesPerPixel * inputWidth;
    
    inputPixels = (UInt32 *)calloc(inputHeight * inputWidth, sizeof(UInt32));
    maskPixels = (UInt32 *)calloc(inputHeight * inputWidth, sizeof(UInt32));
    
    //被裁剪圖片的上下文
    CGContextRef context = CGBitmapContextCreate(inputPixels, inputWidth, inputHeight,
                                                 bitsPerComponent, inputBytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    
    CGContextDrawImage(context, CGRectMake(0, 0, inputWidth, inputHeight), inputCGImage);
    
    //蒙板圖片的上下文
    CGContextRef maskContext = CGBitmapContextCreate(maskPixels, inputWidth, inputHeight,
                                                      bitsPerComponent, inputBytesPerRow, colorSpace,
                                                      kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGContextDrawImage(maskContext, CGRectMake(0, 0, inputWidth, inputHeight), maskCGImage);
    
    // 3. Convert the image
    for (NSUInteger j = 0; j < inputHeight; j++) {
        for (NSUInteger i = 0; i < inputWidth; i++) {
            //獲得源圖片和蒙板圖片每一個(gè)像素值
            UInt32 * currentPixel = inputPixels + (j * inputWidth) + i; 
            UInt32 * currentMaskPixel = maskPixels + (j * inputWidth) + i;
            UInt32 color = *currentMaskPixel;
            NSInteger alpha = A(color); //獲得蒙板上當(dāng)前像素的alpha值
            if (alpha != 0) { //如果不是透明,就修改為透明
                //修改被裁剪圖片當(dāng)前像素的值透明
                *currentPixel = RGBAMake(0, 0, 0, 0);
            }
        }//裁剪
    }
    // 4. Create a new UIImage
    CGImageRef newCGImage = CGBitmapContextCreateImage(context);
    UIImage * processedImage = [UIImage imageWithCGImage:newCGImage];
    
    // 5. Cleanup!
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    CGContextRelease(maskContext);
    free(inputPixels);
    free(maskPixels);
    return processedImage;
}
#undef RGBAMake
#undef R
#undef G
#undef B
#undef A
#undef Mask8

0x10 效果

  1. 蒙板圖片
mengban.png
  1. 裁剪效果
Screen Shot 2016-01-28 at 00.35.19.png

0x11 注意事項(xiàng)和缺點(diǎn)

  1. 使用時(shí)注意要讓裁剪圖的大小與蒙板的圖片大小樣同
  2. 圖片的大小沒(méi)有變小,只是修改了像素的顏色和透明度
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容