2013-05-27 74 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