2013-07-31 167 views
1

我注意到我使用GPUImage的次數越多,我的應用需要的時間越長(使用儀器監視內存使用情況)。GPUImage內存使用情況

作爲一個例子,我使用的每個濾光器在一個不同的方法:

(UIImage*)ToonFilter:(UIImage*)theImage 
    { 
    GPUImageSmoothToonFilter *smoothToonFilter = [[GPUImageSmoothToonFilter alloc] init]; 
    [smoothToonFilter setTexelHeight:0.0025]; 
    [smoothToonFilter setTexelWidth:0.0025]; 
    return [smoothToonFilter imageByFilteringImage:theImage]; 
    } 

    (UIImage*)SketchFilter:(UIImage*)theImage 
    { 
    GPUImageSketchFilter *sketchFilter = [[GPUImageSketchFilter alloc] init]; 
    [sketchFilter setTexelHeight:0.003]; 
    [sketchFilter setTexelWidth:0.003]; 
    return [sketchFilter imageByFilteringImage:theImage]; 
    } 

    (UIImage*)PixellateFilter:(UIImage*)theImage 
    { 
    GPUImagePixellateFilter *pixellateFilter = [[GPUImagePixellateFilter alloc] init]; 
    [pixellateFilter setFractionalWidthOfAPixel:0.01; 
    return [pixellateFilter imageByFilteringImage:theImage]; 
    } 

這是如何使用這些濾波器(testImage是一個UIImage):

testImage = [self SketchFilter:testImage]; 
testImage = [self PixellateFilter:testImage]; 

如果我剛週期通過一遍又一遍的這些過濾器,無需做其他任何事情,應用程序需要越來越多的內存。

我在做什麼錯?我不再需要時如何釋放內存?

+0

您是否找到解決辦法?我遭受同樣的問題。 – MobileMon

回答

0

每次調用這些函數時都會繼續分配新的過濾器。在你的視圖控制器中創建一次,並將它們發送到你的函數,如下面的示例代碼(以及一個GPUImagePicture)。另外不要忘記調用[removeAllTargets]在你的過濾器,有時它們不會從內存中刪除時,他們有目標

(UIImage*)PixellateFilter:(UIImage*)theImage withPixellateFilter:(GPUImagePixellateFilter *) pixellateFilter andStaticPicture:(GPUImagePicture *)staticPicture 

,並從你的過濾器獲得的UIImage之前也使用[過濾prepareForImageCapture]

例如,你可以建立自己的功能像

(UIImage*)PixellateFilter:(UIImage*)theImage withPixellateFilter:(GPUImagePixellateFilter *) pixellateFilter andStaticPicture:(GPUImagePicture *)staticPicture{ 
    [staticPicture removeAllTargets]; 
    UIImage __block *imageToReturn; 
    [staticPicture addTarget:pixellateFilter]; 
    [staticPicture processImageWithCompletionHandler:^{ 
      [pixellateFilter prepareForImageCapture]; 
      imageToReturn = [pixellateFilter imageFromCurrentlyProcessedOutput]; 
      [pixellateFilter removeAllTargets]; 
      pixellateFilter = nil; 
    }]; 
    return imageToReturn; 
} 

我用GPUImageView我的應用程序,它允許您使用GPUImageFilters和GPUImagePictures直接沒有得到UIImage的,你一定要考慮