【问题标题】:Invert colors of image with transparent background反转具有透明背景的图像颜色
【发布时间】:2013-05-02 18:03:31
【问题描述】:

我有一个透明背景的 uiimage。如果我使用以下方法反转颜色,透明背景会变为白色,然后变为黑色。

我希望它不反转透明像素?我该怎么做?

- (UIImage *) getImageWithInvertedPixelsOfImage:(UIImage *)image {
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 2);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference);
    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);
    CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height));
    UIImage * result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return result;
}

【问题讨论】:

    标签: cocoa-touch uiimage transparent


    【解决方案1】:

    我就是这样解决的。

    这是您可以在其他答案中找到的代码,但使用相同图像屏蔽当前图像,并带有旋转上下文(用于从 CG* 坐标转换为 UI* 坐标)。

    - (UIImage *)invertImage:(UIImage *)originalImage
    {
        UIGraphicsBeginImageContext(originalImage.size);
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
        CGRect imageRect = CGRectMake(0, 0, originalImage.size.width, originalImage.size.height);
        [originalImage drawInRect:imageRect];
    
    
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference);
        // translate/flip the graphics context (for transforming from CG* coords to UI* coords
        CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, originalImage.size.height);
        CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
        //mask the image
        CGContextClipToMask(UIGraphicsGetCurrentContext(), imageRect,  originalImage.CGImage);
        CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);
        CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, originalImage.size.width, originalImage.size.height));
        UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return returnImage;
    }
    

    【讨论】:

      【解决方案2】:

      我根据@maggix 的回答创建了一个 Pod:

      pod 'UIImage+InvertedImage'
      

      【讨论】:

        【解决方案3】:

        这是@maggix 答案的 Swift 4.0 版本...

        func invertImage(with originalImage: UIImage) -> UIImage? {
            var image: UIImage?
            UIGraphicsBeginImageContext(originalImage.size)
            if let context = UIGraphicsGetCurrentContext() {
                context.setBlendMode(.copy)
                let imageRect = CGRect(x: 0, y: 0, width: originalImage.size.width, height: originalImage.size.height)
                originalImage.draw(in: imageRect)
                context.setBlendMode(.difference)
                // translate/flip the graphics context (for transforming from CG* coords to UI* coords
                context.translateBy(x: 0, y: originalImage.size.height)
                context.scaleBy(x: 1.0, y: -1.0)
                //mask the image
                context.clip(to: imageRect, mask: originalImage.cgImage!)
                context.setFillColor(UIColor.white.cgColor)
                context.fill(CGRect(x: 0, y:0, width: originalImage.size.width, height: originalImage.size.height))
        
                image = UIGraphicsGetImageFromCurrentImageContext()
            }
            UIGraphicsEndImageContext()
            return image
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-07-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多