[英]Applying CIFilter to UIImageView
在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];
但看起來我缺少某些東西或做錯了什么。
正如另一篇文章所指出的,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
不能將CIImage添加為子視圖,因為它不是視圖(UIView子類)。 您需要一個UIImageView,並將UIImage附加到其“ image”屬性(我相信可以從CIImage創建此UIImage)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.