簡體   English   中英

從CIImage創建UIImage時反轉的顏色(iOS 6.0)

[英]Colors Inverted when Creating UIImage from CIImage (iOS 6.0)

我一直在嘗試大量不同的選項和技巧,以便從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];

產生以下圖像:

倒彩色圖像。

但是,構建這樣的圖像:

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

...產生以下圖像:

在此輸入圖像描述

我嘗試了很多不同的構建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不是......但是,我不知道怎么繞過這個?

這是一個錯誤嗎?

還有一些其他變種:

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

這也是我之前的一點。 我用CIImage s轉換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;
}

這里有一些討論。

看看這個教程。 你必須在它看起來沒問題之前使用過濾器。 希望這可以幫助!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM