创建UIImage从CIImage时颜色反转(iOS 6.0)

5

我一直在尝试各种方法和技巧来使UIImage能够从CIImage正确构建。但是,每次我创建CIImage时,它的颜色似乎都被反转了。

我最近写的代码如下:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"C4Sky" withExtension:@"png"];
CIImage *c = [CIImage imageWithContentsOfURL:url];
UIImage *i = [UIImage imageWithCIImage:c];
UIImageView *uiiv = [[UIImageView alloc] initWithImage:i];
[self.canvas addSubview:uiiv];

这将产生以下图像:

倒置的彩色图像。

然而,构建像这样的图像:

UIImage *i = [UIImage imageNamed:@"C4Sky"];
UIImageView *uiiv = [[UIImageView alloc] initWithImage:i];
[self.canvas addSubview:uiiv];

该代码产生以下图像:

enter image description here

我已经尝试了很多不同的方法来构建CIImage。

这是因为iOS中CIImage的像素格式是ARGB吗?(似乎这是目前iOS6上唯一可能的格式)

如果是这样,如何交换像素格式以使此CIImage看起来正常?



我在git上创建了一个项目存储库,显示了我尝试的所有主要不同方法(不包括每种变体)。有6种方法,前5种失败:

https://github.com/C4Code/CIImageTest

最后一种方法有效,但非常低效:

-(void)test6 {
    UIImage *img = [UIImage imageNamed:@"C4Sky"];

    CGContextRef    bitmapContext = NULL;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;
    CGSize          size = img.size;
    bitmapBytesPerRow   = (size.width * 4);
    bitmapByteCount     = (bitmapBytesPerRow * size.height);
    bitmapData = malloc( bitmapByteCount );
    bitmapContext = CGBitmapContextCreate(bitmapData, size.width, size.height,8,bitmapBytesPerRow,
                                          CGColorSpaceCreateDeviceRGB(),kCGImageAlphaPremultipliedFirst);
    CGContextDrawImage(bitmapContext, (CGRect){CGPointZero,size}, img.CGImage);
    CGImageRef imgRef = CGBitmapContextCreateImage(bitmapContext);

    CIImage *cii = [CIImage imageWithCGImage:imgRef];
    UIImage *finalImage = [UIImage imageWithCIImage:cii];

    UIImageView *uiiv = [[UIImageView alloc] initWithImage:finalImage];
    [self.canvas addSubview:uiiv];
    //works but it's dirrrrrrrty
}

我正在将UIImage绘制到自己的图形上下文中,因为这样做有效,所以我认为CIImage的本地实现可能存在bug。也就是说,从UIImage创建CIImage不起作用... 我能想到的唯一一件事就是CIImage在ARGB空间中,而UIImage不在...但是,我不知道如何解决这个问题?
这是一个bug吗?
3个回答

6

尝试几个额外的变体:

//Grab the CIImage directly from the UIImage
UIImage *img = [UIImage imageNamed:@"C4Sky.png"];
CIImage *c = img.CIImage;
UIImage *i = [UIImage imageWithCIImage:c];
UIImageView *uiiv = [[UIImageView alloc] initWithImage:i];
[self.canvas addSubview:uiiv];

或者

//Maybe you need to set your own color space?
UIImage *img = [UIImage imageNamed:@"C4Sky.png"];
CIImage *c = [CIImage imageWithCGImage:img.CGImage options:@{kCIImageColorSpace: kCGColorSpaceModelRGB}];
UIImage *i = [UIImage imageWithCIImage:c];
UIImageView *uiiv = [[UIImageView alloc] initWithImage:i];
[self.canvas addSubview:uiiv];

-jt


2

这个问题以前也困扰过我。我使用以下方法将CIImage转换为UIImage

- (UIImage *)newUIImageFromCIImage:(CIImage *)image
{
    CGImageRef newImageRef = [[CIContext contextWithOptions:nil] createCGImage:image fromRect:[image extent]]; // you can cache CIContext as it's a little slow to create
    UIImage *newImage = [[UIImage alloc] initWithCGImage:newImageRef];
    CGImageRelease(newImageRef);

    return newImage;
}

Some discussions here.


1

请查看this教程。在它看起来正确之前,您需要应用过滤器。希望这能帮到你!

rachel :)

例如(以防链接损坏)

CIImage *beginImage = [CIImage imageWithContentsOfURL:fileNameAndPath];

CIContext *context = [CIContext contextWithOptions:nil];

CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"
                          keysAndValues: kCIInputImageKey, beginImage,
                @"inputIntensity", @0.8, nil];
CIImage *outputImage = [filter outputImage];


CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];


UIImage *newImage = [UIImage imageWithCGImage:cgimg]; 
self.imageView.image = newImage;


CGImageRelease(cgimg);

嘿,Rachel,谢谢你提供的链接,但是这段代码仍然会产生相同的错误,只是带有一种棕褐色的色调。 - C4 - Travis
是的,我看了一下链接,但似乎没有筛选器可以解决这个问题,如果您知道哪一个会很好。我认为这可能是UIImage构造函数的一个错误,img.CGImage应该生成完全相同图像的核心图形表示...而不是看起来像最上面的图像。 - C4 - Travis
看看这个链接,它更好:https://dev59.com/_Woy5IYBdhLWcg3wnfYi 你可能在做过多的工作。 - Rachel Gallen
这是一个过滤器列表:https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CoreImageFilterReference/Reference/reference.html - Rachel Gallen

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