Making a UIImageView grey

后端 未结 4 921
离开以前
离开以前 2021-01-12 03:35

I\'m setting UImageViews within table cells using setImageWithUrl from the AFNetworking library, but I need the images to be greyscale... is there any way I can do this. I\'

4条回答
  •  悲哀的现实
    2021-01-12 04:15

    I took the code from @Jesse Gumpo's example, above, but here it is as an interface.

    @implementation UIImage ( Greyscale )
    
    - (UIImage *)greyscaleImage
    {
        CIImage * beginImage = [ self CIImage ] ;
        CIImage * evAdjustedCIImage = nil ;
        {
            CIFilter * filter = [ CIFilter filterWithName:@"CIColorControls" 
                                            keysAndValues:kCIInputImageKey, beginImage
                                                , @"inputBrightness", @0.0
                                                , @"inputContrast", @1.1
                                                , @"inputSaturation", @0.0
                                                , nil ] ;
            evAdjustedCIImage = [ filter outputImage ] ;
        }
    
        CIImage * resultCIImage = nil ;
        {
            CIFilter * filter = [ CIFilter filterWithName:@"CIExposureAdjust" 
                                            keysAndValues:kCIInputImageKey, evAdjustedCIImage
                                                , @"inputEV", @0.7
                                                , nil ] ;
            resultCIImage = [ filter outputImage ] ;
        }
    
        CIContext * context = [ CIContext contextWithOptions:nil ] ;
        CGImageRef resultCGImage = [ context createCGImage:resultCIImage 
                                                  fromRect:resultCIImage.extent ] ;
        UIImage * result = [ UIImage imageWithCGImage:resultCGImage ] ;
        CGImageRelease( resultCGImage ) ;
    
        return result;
    }
    
    @end
    

    Now you can just do this:

    UIImage * downloadedImage = ... get from AFNetwork results ... ;
    downloadedImage = [ downloadedImage greyscaleImage ] ;
    
    ... use 'downloadedImage' ...
    

提交回复
热议问题