2013-05-27 31 views
0

在UIViewController中显示UIImageView中的图像。我想用一些特殊效果来显示它。使用核心图像。框架在UIImageView中应用CIFilter

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

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

回答

1

CIImage不能作为子视图添加,因为它不是视图(UIView子类)。你需要一个带有UIImage的UIImageView,它的'image'属性(我相信你可以从CIImage创建UIImage)。

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,因为它不是一个视图本身!

欲了解更多信息,请参阅核心图像编程指南:https://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/CoreImaging/ci_intro/ci_intro.html