SDWebImage 获得网络图片后再置灰

获得图片之后进行回调

[_TrainPic sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:IMG(@"Train_level_no_1") completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    _TrainPic.image = [self getGrayImage:_TrainPic.image];
                }];

置灰方法

- (UIImage*)getGrayImage:(UIImage*)sourceImage
{
    int width = sourceImage.size.width;
    int height = sourceImage.size.height;
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate (nil,width,height,8,0,colorSpace,kCGImageAlphaNone);
    CGColorSpaceRelease(colorSpace);
    
    if (context == NULL) {
        return nil;
    }
    
    CGContextDrawImage(context,CGRectMake(0, 0, width, height), sourceImage.CGImage);
    CGImageRef grayImageRef = CGBitmapContextCreateImage(context);
    UIImage *grayImage = [UIImage imageWithCGImage:grayImageRef];
    CGContextRelease(context);
    CGImageRelease(grayImageRef);
    
    return grayImage;
}

小白总结,欢迎打脸指正

你可能感兴趣的:(SDWebImage 获得网络图片后再置灰)