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. 图片的大小没有变小,只是修改了像素的颜色和透明度

转载于:https://www.cnblogs.com/LeeYZ/p/5165019.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值