iOS_简单的把图片渲染成灰色

- (UIImage *)getGrayImageFrom:(UIImage *)img{
    
    int imgW = img.size.width;
    int imgH = img.size.height;
    
    CGColorSpaceRef spaceRef = CGColorSpaceCreateDeviceGray();
    
    CGContextRef context = CGBitmapContextCreate(nil, imgW, imgH, 8, 0, spaceRef, kCGBitmapByteOrderDefault);
    if (context == NULL) {
        return  nil;
    }
    
    CGRect rect = CGRectMake(0, 0, imgW, imgH);
    CGContextDrawImage(context, rect, img.CGImage);
    UIImage *grayImg = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
    
    CGColorSpaceRelease(spaceRef);
    CGContextRelease(context);
    
    return grayImg;
}

你可能感兴趣的:(iOS_简单的把图片渲染成灰色)