我一直在尝试大量不同的选项和技巧,以便从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不是......但是,我不知道如何解决这个问题?
这是一个错误吗?
答案 0 :(得分: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
答案 1 :(得分:2)
这也是我之前的一点。我用UIImage
s转换CIImage
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。
答案 2 :(得分:1)
查看this教程。你必须在它看起来没问题之前使用过滤器。希望这有帮助!
rachel:)
e.g。 (以防链接中断)
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);