0

在 UIViewController 在 UIImageView 中显示图像。我想用一些特殊效果来显示它。使用核心 image.framework

UIImageView *myImage = [[UIImageView alloc]
                       initWithImage:[UIImage imageNamed:@"piano.png"]];

myImage.frame = CGRectMake(10, 50, 300, 360);

CIContext *context = [CIContext contextWithOptions:nil];

CIFilter *filter= [CIFilter filterWithName:@"CIVignette"];

CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"piano.png"]];

[filter setValue:inputImage forKey:@"inputImage"];

[filter setValue:[NSNumber numberWithFloat:18] forKey:@"inputIntensity"];

[filter setValue:[NSNumber numberWithFloat:0] forKey:@"inputRadius"];

[baseView addSubview:inputImage];

但看起来我错过了什么或做错了什么。

4

2 回答 2

3

正如另一篇文章所指出的,CIImage 不是视图,因此不能将其添加为一个视图。CIImage 实际上仅用于进行图像处理,要显示过滤后的图像,您需要将其转换回 UIImage。为此,您需要从过滤器(而不是输入图像)中获取输出 CIImage。如果您链接了多个过滤器,请使用链中的最后一个过滤器。然后您需要将输出 CIImage 转换为 CGImage,然后从那里转换为 UIImage。这段代码完成了这些事情:

CIImage *result = [filter valueForKey:kCIOutputImageKey]; //Get the processed image from the filter

CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]; //Create a CGImage from the output CIImage

UIImage* outputImage = [UIImage imageWithCGImage:cgImage]; // Create a UIImage from the CGImage

还要记住,UIImage 必须进入 UIImageView,因为它本身不是视图!

有关更多信息,请参阅 Core Image 编程指南:https ://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/CoreImaging/ci_intro/ci_intro.html

于 2013-05-28T00:31:00.580 回答
1

CIImage 不能作为子视图添加,因为它不是视图(UIView 子类)。您需要一个 UIImageView,其 UIImage 附加到其“图像”属性(我相信您可以从 CIImage 创建此 UIImage)。

于 2013-05-27T23:12:47.710 回答