2013-05-02 51 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 
}