ios 图片灰度处理

+(UIImage *)changeGrayImage:(UIImage *)oldImage {
    int bitmapInfo = kCGImageAlphaPremultipliedLast;
    int width = oldImage.size.width;
    int height = oldImage.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate (nil, width, height, 8, 0, colorSpace, bitmapInfo);
    CGColorSpaceRelease(colorSpace);
    if (context == NULL) {
        return nil;
        
    }
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), oldImage.CGImage);
    UIImage *grayImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
    CGContextRelease(context);
    return grayImage;
    
}

bitmapInfo

可以自己改变这个值,来决定png图片的的透明区域是否也要跟着一起灰度处理。

更多问题,加群讨论:565191947

猜你喜欢

转载自blog.csdn.net/a787188834/article/details/80737013