iOS图片的伪裁剪(改变图片的像素值)

0x00 原理

利用一张图片事先画好的图片(以下称为蒙板),盖在要被裁剪的的图片上,然后遍历蒙板上的像素点,修改被裁剪图片对应位置的像素的色值即可得到一些我们想要的不规则图片了(比如人脸)

0x01 代码实现(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];//准备被裁剪的图片
    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++) {
            //获得源图片和蒙板图片每一个像素值
            UInt32 * currentPixel = inputPixels + (j * inputWidth) + i; 
            UInt32 * currentMaskPixel = maskPixels + (j * inputWidth) + i;
            UInt32 color = *currentMaskPixel;
            NSInteger alpha = A(color); //获得蒙板上当前像素的alpha值
            if (alpha != 0) { //如果不是透明,就修改为透明
                //修改被裁剪图片当前像素的值透明
                *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 注意事项和缺点

  1. 使用时注意要让裁剪图的大小与蒙板的图片大小样同
  2. 图片的大小没有变小,只是修改了像素的颜色和透明度
posted @ 2016-01-28 00:45  LetGo  阅读(680)  评论(0编辑  收藏  举报