13

我发现

通过向现有 UIImage 添加阴影来创建新的 UIImage

UIImage,有没有一种简单的方法可以使它变暗或全黑

但是选定的答案对我不起作用。

我有一个 UIImage,其中可能有一些透明像素,我需要创建一个新的 UIImage,其中不透明的像素变暗,有什么办法吗?我正在考虑使用 UIBezierPath 但我不知道如何仅对不透明像素执行此操作。

4

4 回答 4

33

这是我用来为图像着色的类,即使它们是透明的。

+ (UIImage *)colorizeImage:(UIImage *)image withColor:(UIColor *)color {
    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect area = CGRectMake(0, 0, image.size.width, image.size.height);

    CGContextScaleCTM(context, 1, -1);
    CGContextTranslateCTM(context, 0, -area.size.height);

    CGContextSaveGState(context);
    CGContextClipToMask(context, area, image.CGImage);

    [color set];
    CGContextFillRect(context, area);

    CGContextRestoreGState(context);

    CGContextSetBlendMode(context, kCGBlendModeMultiply);

    CGContextDrawImage(context, area, image.CGImage);

    UIImage *colorizedImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return colorizedImage;
}

要使图像变暗,您将向该方法传递具有较低透明度的黑色或灰色 UIColor。

于 2012-09-12T02:21:19.230 回答
16

试试 CoreImage 过滤器怎么样?

您可以使用 CIColorControls 过滤器来调整输入亮度和对比度以使图像变暗。

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]];

在此处阅读有关 CIFilter 参数的更多信息:

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

于 2012-09-12T02:24:49.943 回答
10

这是一个快速的 Swift 版本,使用 CIExposureAdjust CIFilter :)

  // Get the original image and set up the CIExposureAdjust filter
  guard let originalImage = UIImage(named: "myImage"),
    let inputImage = CIImage(image: originalImage),
    let filter = CIFilter(name: "CIExposureAdjust") else { return }

  // The inputEV value on the CIFilter adjusts exposure (negative values darken, positive values brighten)
  filter.setValue(inputImage, forKey: "inputImage")
  filter.setValue(-2.0, forKey: "inputEV")

  // Break early if the filter was not a success (.outputImage is optional in Swift)
  guard let filteredImage = filter.outputImage else { return }

  let context = CIContext(options: nil)
  let outputImage = UIImage(CGImage: context.createCGImage(filteredImage, fromRect: filteredImage.extent))

  myImageView.image = outputImage // use the filtered UIImage as required.
于 2016-01-12T23:32:31.533 回答
-1

这是大卫在 Swift 5 中的回答:

class func colorizeImage(_ image: UIImage?, with color: UIColor?) -> UIImage? {
    UIGraphicsBeginImageContextWithOptions(image?.size ?? CGSize.zero, _: false, _: image?.scale ?? 0.0)

    let context = UIGraphicsGetCurrentContext()
    let area = CGRect(x: 0, y: 0, width: image?.size.width ?? 0.0, height: image?.size.height ?? 0.0)

    context?.scaleBy(x: 1, y: -1)
    context?.translateBy(x: 0, y: -area.size.height)

    context?.saveGState()
    context?.clip(to: area, mask: (image?.cgImage)!)

    color?.set()
    context?.fill(area)

    context?.restoreGState()

    if let context = context {
        context.setBlendMode(.multiply)
    }

    context!.draw((image?.cgImage)!, in: area)

    let colorizedImage = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    return colorizedImage
}
于 2020-02-27T04:12:52.250 回答