首页 文章

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

提问于
浏览
5

我一直在尝试大量不同的选项和技巧,以便从CIImage中正确构建UIImage . 但是,每次我创建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];

这产生了以下图像:

The inverted color image.

但是,构建这样的图像:

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的本机实现可能存在错误 . 也就是说,从UIImage创建一个CIImage是行不通的......再次,我唯一能想到的是CIImage在ARGB空间,而UIImage不是......但是,我不知道怎么解决这个问题?

这是一个错误吗?

3 回答

  • 2

    还有一些其他变种:

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

  • 1

    这也是我之前的一点 . 我用 CIImage 转换了 UIImage s,结果如下:

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

    一些讨论here .

  • 6

    看看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);
    

相关问题