[英]CIFilter output image is showing previous output image at random
我發現帶有CIGaussianBlur過濾器的CIFilter行為很奇怪。
我針對不同的圖像多次快速執行此方法。 有時,將返回“最后處理的圖像”,而不是我發送的圖像。例如,如果我有圖像:
A , B和C。
如果我連續快速執行模糊處理,有時會得到如下結果:
模糊A , 模糊A , 模糊C
+(UIImage *)applyBlurToImageAtPath:(NSURL *)imageUrlPath
{
if (imageUrlPath == nil)
return nil;
//Tried to create new contexts each loop, and also tried to use a singleton context
// if(CIImageContextSingleton == nil)
// {
// CIImageContextSingleton = [CIContext contextWithOptions:nil];
// }
CIContext *context = [CIContext contextWithOptions:nil];//[Domain sharedInstance].CIImageContextSingleton;
CIFilter *gaussianBlurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
[gaussianBlurFilter setDefaults];
CIImage *inputImage = [CIImage imageWithContentsOfURL:imageUrlPath];
[gaussianBlurFilter setValue:inputImage forKey:kCIInputImageKey];
[gaussianBlurFilter setValue:@(1) forKey:kCIInputRadiusKey];
//Tried both these methods for getting the output image
CIImage *outputImage = [gaussianBlurFilter valueForKey:kCIOutputImageKey];
// CIImage *outputImage = [gaussianBlurFilter outputImage];
//If I'm doing this, the problem never occurs, so the problem is isolated to the gaussianBlurFilter:
//outputImage = inputImage;
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[inputImage extent]];
UIImage *resultImage = [UIImage imageWithCGImage:cgimg];
//Tried both with and without releasing the cgimg
CGImageRelease(cgimg);
return resultImage;
}
我已經嘗試過一次循環,並且在做手勢之類時通過運行方法而出現了相同的問題。 (imageUrlPath上的圖像是正確的。)此外,請參閱代碼中有關我嘗試過的內容的注釋。
我想念什么嗎? CIFilter是否有一些內部緩存? 該方法始終在主線程上運行。
根據給出的代碼,並假設始終在主線程上調用此方法,您應該可以,但我確實看到了代碼中不建議的某些事情:
CIContext
。 我建議以一種不同的方式進行結構設計,而不是作為一個Singleton。 進行大量渲染時,請保持CIContext
周圍,並重復使用相同的上下文。 CIFilter
沒有更改,則也不必每次都重新創建它。 如果要在同一線程上調用該方法,則只需在過濾器上設置inputImage
鍵。 每當輸入圖像更改時,您都需要從過濾器中獲取新的outputImage
。 我的猜測是問題可能是圍繞核心圖像上下文渲染到相同的基礎圖形環境(可能是GPU渲染),但是由於您一直在不斷重新創建CIContext,因此可能發生了一些不可思議的事情。
只是一個猜測,因為我沒有方便的代碼來測試自己。 如果您有一個演示該問題的測試項目,則調試起來會更容易。 另外-我仍然對線程持懷疑態度。 它在不應用模糊的情況下有效的事實並不一定證明它是導致問題的模糊-我的經驗中,隨機性更可能涉及線程問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.