2013-05-02 106 views
3

我有一个透明背景的图像。如果我用后续方法反转颜色,透明背景会变成白色,然后变为黑色。颠倒透明背景图像的颜色

我希望它不反转透明像素?我怎样才能做到这一点?

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

}

谢谢!

+0

嗨,你有没有找到任何解决方案这个问题 ?? – Aziz 2013-06-11 20:27:50

回答

2

这就是我解决问题的方法。

这是你可以在其他答案中找到的代码,但是用旋转的上下文(用于从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; 
} 
1

我创建了一个吊舱从@ maggix的回答是:

pod 'UIImage+InvertedImage' 
0

这是雨燕4.0版本@maggix回答...

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 
}