iphone - 是否可以以编程方式反转图像的颜色?

标签 iphone ios ipad uiimage

我想在 iOS 中拍摄图像并反转颜色。

最佳答案

为了扩展 quixoto 的答案,并且因为我有我自己项目的相关源代码,如果您需要转到 on-CPU 像素操作,那么我已经添加了说明的以下内容应该执行技巧:

@implementation UIImage (NegativeImage)

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

            // 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;
}

@end

因此向 UIImage 添加了一个类别方法:

  1. 创建一个清晰的 CoreGraphics 位图上下文,它可以访问其内存
  2. 为它绘制 UIImage
  3. 遍历每个像素,从预乘的 alpha 转换为未变形的 RGB,分别反转每个 channel ,再次乘以 alpha 并存储回去
  4. 从上下文中获取图像并将其包装到 UIImage 中
  5. 自行清理并返回 UIImage

关于iphone - 是否可以以编程方式反转图像的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6672517/

相关文章:

ios - 上下文闭包类型 Response<AnyObject> -> void 需要 1 个 agrument 但 3 个在闭包体中

ios - webViewDidFinishLoad() 似乎没有在 Swift 中运行

css - iPad css 设计 - 无法 overflow hidden

iphone - RadioKit替代品,用于从URL实时流式传输音频

ios - 如何在另一个 Storyboard中添加 Storyboard引用

iphone - 如何在 iPhone cocos2d 中移动水?

iphone - 使用 MapKit 检索 UserLocation 坐标

iphone - 如何将 uitableview 选择与 indexpath 值居中

ios - Match-O Linker错误,在我 friend 的Mac上构建,而不在我的Mac上构建

ios - 无法在 iPad 上输入空的 TinyMCE 4 区域