将白色反转为黑色的UIImage

7
我有以下UIImage:

enter image description here

使用Objective-C,我想要能够将白色反转为黑色,反之亦然。
任何帮助都将不胜感激。

你是指黑色背景和白色签名吗? - Shashi3456643
@Shashi3456643,是的,那就是我需要的。 - Maystro
5个回答

9

Swift

使用CIContext代替-UIImage:CIImage(请参阅https://dev59.com/gIfca4cB1Zd3GeqPlqY6#28386697),并在@wtznc的回答的基础上构建,这里是一个自包含的IBDesignable

@IBDesignable
class InvertImage: UIImageView {

    @IBInspectable var originalImage:UIImage? = nil

    @IBInspectable var invert:Bool = false {
        didSet {
            var inverted = false
            if let originalImage = self.originalImage {
                if(invert) {
                    let image = CIImage(CGImage: originalImage.CGImage!)
                    if let filter = CIFilter(name: "CIColorInvert") {
                        filter.setDefaults()
                        filter.setValue(image, forKey: kCIInputImageKey)
                        
                        let context = CIContext(options: nil)
                        let imageRef = context.createCGImage(filter.outputImage!, fromRect: image.extent)
                        self.image = UIImage(CGImage: imageRef)
                        inverted = true
                    }
                }
            }
            
            if(!inverted) {
                self.image = self.originalImage
            }
        }
    }
}

要使用它,请设置原始图像,而不是图像,因为图像将被动态关联:

输入图片描述


8

Swift3

extension UIImage {
    func invertedImage() -> UIImage? {
        guard let cgImage = self.cgImage else { return nil }
        let ciImage = CoreImage.CIImage(cgImage: cgImage)
        guard let filter = CIFilter(name: "CIColorInvert") else { return nil }
        filter.setDefaults()
        filter.setValue(ciImage, forKey: kCIInputImageKey)
        let context = CIContext(options: nil)
        guard let outputImage = filter.outputImage else { return nil }
        guard let outputImageCopy = context.createCGImage(outputImage, from: outputImage.extent) else { return nil }
        return UIImage(cgImage: outputImageCopy, scale: self.scale, orientation: .up) 
    }
}

1
不知何故,这会影响图像(显示)大小。 - Sjakelien
@Peter 我很好奇。自从我上次使用核心图像以来已经过了一段时间,所以我无法找到它。你有解决办法吗? - neoneye
好的,我找到了内存泄漏的问题 - 函数返回的UI图像会保留dataProvider在内存中。我的代码将图像数据保存在图形内存中,因此使系统内存使用率低。 - Peter
@Peter,你解决了吗?你使用Instruments来查找内存泄漏吗?有时我会出现保留循环,甚至没有意识到。很抱歉这给你带来了麻烦。 - neoneye
没关系,你的代码是推荐的方法。我使用工具来查看是什么在占用内存,然后使用XCode中的内存图来追踪源头。我的解决方案并不容易——我必须修改我在CGContext中生成的图像,虽然写起来很困难,但从内存角度来看,这是最好的解决方案。我使用的代码基于https://stackoverflow.com/a/25798154/460053。 - Peter
显示剩余2条评论

5
- (UIImage *)negativeImage
{
    // get width and height as integers, since we'll be using them as
    // array subscripts, etc, and this'll save a whole lot of casting
    CGSize size = self.size;
    int width = size.width;
    int height = size.height;

    // Create a suitable RGB+alpha bitmap context in BGRA colour space
    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *memoryPool = (unsigned char *)calloc(width*height*4, 1);
    CGContextRef context = CGBitmapContextCreate(memoryPool, width, height, 8, width * 4, colourSpace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(colourSpace);

    // draw the current image to the newly created context
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), [self CGImage]);

    // run through every pixel, a scan line at a time...
    for(int y = 0; y < height; y++)
    {
        // get a pointer to the start of this scan line
        unsigned char *linePointer = &memoryPool[y * width * 4];

        // step through the pixels one by one...
        for(int x = 0; x < width; x++)
        {
            // get RGB values. We're dealing with premultiplied alpha
            // here, so we need to divide by the alpha channel (if it
            // isn't zero, of course) to get uninflected RGB. We
            // multiply by 255 to keep precision while still using
            // integers
            int r, g, b;
            if(linePointer[3])
            {
                r = linePointer[0] * 255 / linePointer[3];
                g = linePointer[1] * 255 / linePointer[3];
                b = linePointer[2] * 255 / linePointer[3];
            }
            else
                r = g = b = 0;

            // perform the colour inversion
            r = 255 - r;
            g = 255 - g;
            b = 255 - b;

            if ( (r+g+b) / (3*255) == 0 )
            {

                linePointer[0] = linePointer[1] = linePointer[2] = 0;
                linePointer[3] = 0;
            }
            else
            {
                // multiply by alpha again, divide by 255 to undo the
                // scaling before, store the new values and advance
                // the pointer we're reading pixel data from
                linePointer[0] = r * linePointer[3] / 255;
                linePointer[1] = g * linePointer[3] / 255;
                linePointer[2] = b * linePointer[3] / 255;

            }
            linePointer += 4;
        }
    }

    // get a CG image from the context, wrap that into a
    // UIImage
    CGImageRef cgImage = CGBitmapContextCreateImage(context);
    UIImage *returnImage = [UIImage imageWithCGImage:cgImage];

    // clean up
    CGImageRelease(cgImage);
    CGContextRelease(context);
    free(memoryPool);

    // and return
    return returnImage;
}

我在UIImage扩展类中添加了上述方法。


构建错误在[self CGImage]处:'IndexViewController'没有声明选择器'CGImage'的可见@interface。 - Gank
1
你说得对。我忘了提到这个方法是在UIImage类的扩展中... - Maystro
这个怎么用 Swift 写? - Khaled Annajar

4
首先,您需要将核心图像框架(Core Image framework)添加到您的项目中。
项目设置 -> 目标 "项目名称" -> 构建阶段 -> 链接二进制文件 -> 添加项目 -> CoreImage.framework
其次,在您的实现文件中导入核心图像头文件(Core Image header)
#import <CoreImage/CoreImage.h>

初始化一个UIImage对象来存储原始文件。

UIImage *inputImage = [UIImage imageNamed:@"imageNamed"];

创建一个 CIFilter,定义您想要如何修改原始的 UIImage 对象。
CIFilter* filter = [CIFilter filterWithName:@"CIColorInvert"];
[filter setDefaults];
[filter setValue:inputImage.CIImage forKey:@"inputImage"];

创建另一个UIImage对象来保存修改后的图像。
UIImage *outputImage = [[UIImage alloc] initWithCIImage:filter.outputImage];

完成!希望能对您有所帮助。


你能发布一张你正在测试的图片吗? - Maystro

2

针对 Xamarin C#(仅限iOS,不是跨平台解决方案):

public static UIImage InvertImageColors( UIImage original )
{
    return ApplyFilter( original, new CIColorInvert() );
}

public static UIImage ApplyFilter( UIImage original, CIFilter filter )
{
    UIImage result;
    try {
        CIImage coreImage = original.CGImage;
        filter.SetValueForKey( coreImage, CIFilterInputKey.Image );
        CIImage output = filter.OutputImage;
        CIContext context = CIContext.FromOptions( null );
        CGImage cgImage = context.CreateCGImage( output, output.Extent );
        result = UIImage.FromImage( cgImage );

    } catch (Exception ex) {
        // .. Log error here ..
        result = original;
    }

    return result;
}

注意1:这是从别人的回答中改编而来,我已经追不上原始来源了。
注意2:故意每行只做一小步,以便您可以看到中间类型,并轻松地适应其他情况。

另请参见Xamarin文档 - CIFilter类


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接