美文网首页iOS Developer
SDWebImage 获得网络图片后再置灰

SDWebImage 获得网络图片后再置灰

作者: Chase_Eleven | 来源:发表于2017-06-30 14:41 被阅读0次

    获得图片之后进行回调

    [_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 获得网络图片后再置灰

        本文链接:https://www.haomeiwen.com/subject/gnzbcxtx.html