2013-05-02 3 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

ответ

2

так я решил.

Это код, который вы можете найти в других ответах, но с маскировкой текущего изображения с тем же изображением с поворотным контекстом (для преобразования из CG * коордов в UI * coords).

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

Это стремительная версия @maggix ответа 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 
} 
Смежные вопросы