iOS Developer

SDWebImage 获得网络图片后再置灰

2017-06-30  本文已影响0人  Chase_Eleven

获得图片之后进行回调

[_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;
}

小白总结,欢迎打脸指正

上一篇下一篇

猜你喜欢

热点阅读