[英]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.