来自 NSURL 的 CGImage 有效但不适用于 UIImage [英] CGImage from NSURL works but not from UIImage
本文介绍了来自 NSURL 的 CGImage 有效但不适用于 UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在将过滤器应用于图像时遇到了一些问题.以下代码在使用 CIImage:imageWithContentsOfURL 时完美运行:
I have some issues applying filters to an image. The following code works perfect when using CIImage:imageWithContentsOfURL:
NSInteger filterIndex = [(UITapGestureRecognizer *)sender view].tag;
Filter *filter = [filters objectAtIndex:filterIndex];
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *image = [CIImage imageWithContentsOfURL:[NSURL URLWithString:@"http://url.to/photo.jpg"]];
[filter.filter setValue:image forKey:kCIInputImageKey];
CIImage *result = [filter.filter valueForKey:kCIOutputImageKey];
CGRect extent = [result extent];
CGImageRef cgImage = [context createCGImage:result fromRect:extent];
[self.imageView setImage:[UIImage imageWithCGImage:cgImage]];
Filter 只是一个包含过滤器名称和 CIFilter 的模型:
Filter is just a model holding filter name and CIFilter:
-(id)initWithNameAndFilter:(NSString *)theName filter:(CIFilter *)theFilter;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) CIFilter *filter;
但是当我尝试使用以下代码加载本地 UIImage 时:
But when I try to load a local UIImage with the following code:
CIImage *ciImage = [UIImage imageNamed:@"test.png"].CIImage;
过滤后的图像只会变白(或消失).我做错了什么?
The filtered image only turns white (or disappears). What am I doing wrong?
推荐答案
Juraj Antas 在这个问题中的回答 如何将 UIImage 转换为 CIImage,反之亦然 解决了我的问题:
Juraj Antas' answer in this question How to Convert UIImage to CIImage and vice versa fixed my issue:
UIImage *u = [UIImage imageNamed:@"image.jpg"];
CIImage *image = [[CIImage alloc] initWithCGImage: u.CGImage];
这篇关于来自 NSURL 的 CGImage 有效但不适用于 UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文