iOS: create an darker version of UIImage and leave transparent pixels unchanged?

前端 未结 4 481
小蘑菇
小蘑菇 2021-02-05 16:31

I found

Create new UIImage by adding shadow to existing UIImage

and

UIImage, is there an easy way to make it darker or all black

But the selected

4条回答
  •  野的像风
    2021-02-05 17:26

    How about trying a CoreImage Filter?

    You could use the CIColorControls filter to adjust the input brightness and contrast to darken the image.

    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *inputImage = [[CIImage alloc] initWithImage:sourceImage]; //your input image
    
    CIFilter *filter= [CIFilter filterWithName:@"CIColorControls"];
    [filter setValue:inputImage forKey:@"inputImage"];
    [filter setValue:[NSNumber numberWithFloat:0.5] forKey:@"inputBrightness"];
    
    // Your output image
    UIImage *outputImage = [UIImage imageWithCGImage:[context createCGImage:filter.outputImage fromRect:filter.outputImage.extent]];
    

    Read more about the CIFilter parameters here:

    http://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CoreImageFilterReference/Reference/reference.html%23//apple_ref/doc/filter/ci/CIColorControls

提交回复
热议问题