2012-05-31 48 views
1

我正在使用圖像過濾器應用程序。在iOS 5中顯示從CIImage獲得的UIImage核心圖像過濾器

context = [CIContext contextWithOptions:nil]; 



    beginImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"biscus_small.png"]]; 


    filter = [CIFilter filterWithName:@"CISepiaTone" 
            keysAndValues: kCIInputImageKey, beginImage, 
         @"inputIntensity", [NSNumber numberWithFloat:0.8], nil]; 

然後,當用戶選擇的過濾器,我只是得到了CIImage過濾器已申請後分配到的UIImageView:我一直在使用下面的代碼創建的過濾器。出於某種原因,setImage之後的UIImageView顯示爲白色(無)。

CIImage *outputImage = [filter outputImage]; 

    // CGImageRef cgimg = 
    // [context createCGImage:outputImage fromRect:[outputImage extent]]; 

    UIImage *finalImage = [UIImage imageWithCIImage:outputImage]; 

    [self.imageView setImage:finalImage]; 

如果我使用CIContext的createCGImage方法,然後正常工作,並顯示過濾器的圖像,但使用createCGImage是極其緩慢的。

回答

6

CIImage的-createCGImage:fromRect:太慢的原因是這實際上觸發了柵格位圖的處理和輸出。將輸出CIImage分配給UIImage不會導致處理和輸出圖像。通過核心圖像過濾圖像時,您將無法避免此步驟。

正如我在this answer中所述,Core Image在iOS上的表現有點令人失望,從iOS 5.1開始。如果我可以提出建議,我已經構建了an open source framework,它可以進行GPU加速的圖像處理,並在幾乎所有我測試過的環境下都能夠擊敗Core Image的性能。對於圖像過濾,我比iPhone 4上的Core Image快大約4倍。

但是,在過濾時仍然會有一些開銷,因此如果您有能力使用raw圖像字節,我的原始數據輸入和輸出比處理UIImages要快得多。我將在此框架中添加一些直接的JPEG和PNG輸入和輸出,以便爲此提供更快的路徑,而不必經過UIImage。

+0

非常感謝Brad! BTW愛愛iTunesU上你的課程。非常感謝你! – azamsharp

相關問題