Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c iOS:创建更暗版本的UIImage,并保持透明像素不变?_Objective C_Ios_Uiimage_Core Graphics - Fatal编程技术网

Objective c iOS:创建更暗版本的UIImage,并保持透明像素不变?

Objective c iOS:创建更暗版本的UIImage,并保持透明像素不变?,objective-c,ios,uiimage,core-graphics,Objective C,Ios,Uiimage,Core Graphics,我发现 及 但是选择的答案对我来说并不适用 我有一个UIImage,其中可能有一些透明像素,我需要创建一个新的UIImage,非透明像素变暗,有没有办法做到这一点?我曾考虑使用UIBezierPath,但我不知道如何仅对不透明像素使用它。这是我用来给图像着色的类,即使它们是透明的 + (UIImage *)colorizeImage:(UIImage *)image withColor:(UIColor *)color { UIGraphicsBeginImageContextWit

我发现

但是选择的答案对我来说并不适用


我有一个UIImage,其中可能有一些透明像素,我需要创建一个新的UIImage,非透明像素变暗,有没有办法做到这一点?我曾考虑使用UIBezierPath,但我不知道如何仅对不透明像素使用它。

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

+ (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方法。

试试CoreImage过滤器怎么样

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

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参数的更多信息:


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


以下是大卫在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
}

很好的解决方案,但请注意,它需要iOS 5。此外,您的代码存在严重内存泄漏(必须发布CreateCImage!),而且(对我来说)不起作用-接受的答案非常有效。谢谢!建议的改进:使用
UIGraphicsBeginImageContextWithOptions(image.size,NO,image.scale)
保留比例,否则彩色图像在视网膜显示上会模糊。@Merott我将继续编辑答案以包含您的建议。它可能被版主拒绝了,但是如果OPs满意的话,希望他们能修复它。这是非常有帮助的。非常感谢。
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
}