2009-09-17 19 views
0

是否有其他人遇到過這個問題? ObjectAlloc由於CGBitmapContextCreateImage而上升。蘋果的軟件沒有完全釋放objectalloc嗎?iPhone - 多個CGBitmapContextCreateImage調用 - ObjectAlloc攀登

我用NSTimer每秒調整圖像大小12次。在調整圖像大小的過程中,我還通過添加了插值質量來添加一個像高斯模糊效果一樣的photoshop。

使用儀器後,它不顯示任何內存泄漏,但我的objectalloc只是繼續攀升。它直接指向CGBitmapContextCreateImage
CGBitmapContextCreateImage> CREATE_ bitmap_ data_provide>的malloc

任何人都知道一個解決方案嗎?或甚至可能的想法?

該呼叫的NSTimer

NSString * fileLocation = [[NSBundle mainBundle] pathForResource:imgMain ofType:@"jpg"]; 
NSData * imageData = [NSData dataWithContentsOfFile:fileLocation]; 
UIImage * blurMe = [UIImage imageWithData:imageData]; 

CGRect rect = CGRectMake(0, 0, round(blurMe.size.width /dblBlurLevel), round(blurMe.size.width /dblBlurLevel)); 
UIImage * imageShrink = [self resizedImage: blurMe : rect : 3.0]; 

CGRect rect2 = CGRectMake(0, 0, blurMe.size.width , blurMe.size.width); 
UIImage * imageReize = [self resizedImage: imageShrink : rect2 : 3.0]; 

imgView.image = imageReize; 

大小功能

-(UIImage *) resizedImage:(UIImage *)inImage : (CGRect)thumbRect : (double)interpolationQuality 
{ 
    CGImageRef     imageRef = [inImage CGImage]; 
    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); 

    if (alphaInfo == kCGImageAlphaNone) 
     alphaInfo = kCGImageAlphaNoneSkipLast; 

    // Build a bitmap context that's the size of the thumbRect 
    CGContextRef bitmap = CGBitmapContextCreate(
           NULL, 
           thumbRect.size.width, 
           thumbRect.size.height,   
           CGImageGetBitsPerComponent(imageRef), 
           4 * thumbRect.size.width,  
           CGImageGetColorSpace(imageRef), 
           alphaInfo 
           ); 

    // Draw into the context, this scales the image 
    CGContextSetInterpolationQuality(bitmap, interpolationQuality); 
    CGContextDrawImage(bitmap, thumbRect, imageRef); 

    // Get an image from the context and a UIImage 
    CGImageRef ref = CGBitmapContextCreateImage(bitmap); 
    UIImage* result = [UIImage imageWithCGImage:ref]; 

    CGContextRelease(bitmap); // ok if NULL 
    CGImageRelease(ref); 

    return [result autorelease]; 
} 
+0

您確定CGContextSetInterpolationQuality會產生高斯模糊嗎? – mahboudz 2009-09-17 03:03:29

+0

你能告訴你如何調用這個方法以及你如何處理返回的圖像? – coneybeare 2009-09-17 03:26:54

+0

是的,我使用這個功能的方式非常適合像高斯模糊一樣的photoshop。我將一個普通的UIImage傳遞到這個腳本中,並將圖像的大小調整爲非常小的尺寸,併爲其添加一些InterpolationQuality。這樣做會使新的非常小的圖像模糊。然後,我再次調用resize腳本,並將其傳遞到剛剛創建的縮小且模糊的圖像中。當你放大小的模糊圖像時,它回來了一個完美的高斯模糊。我已經搜索了各地找到一個簡單的方法來添加一個高斯模糊,但我還沒有找到一個功能添加一個這樣的好。 – bbullis21 2009-09-17 03:27:50

回答

1

內的代碼overreleases result

也就是說,問題很可能是UIImage沒有被取消分配,並且UIImage持有CGImage,而CGImage持有CGBitmapContextCreate下分配的內存。

使用工具來查看UIImages是否沒有被釋放,如果是的話嘗試調試原因。

+0

爲了確保我不會過度釋放結果,我可以只替換.... return [result autorelease]和返回結果; ....如果是這樣的話,那也不能解決它。我原來的代碼是這樣的,有人給出了添加autorelease的建議。這個CGBitmapContextCreateImage> create_bitmap_data_provide> malloc讓我瘋狂。 – bbullis21 2009-09-17 03:07:51

+0

過度銷售不是問題,這是其他問題。 這是我的評論的其餘部分是相關的。檢查你是否泄露UIImages。 – Ken 2009-09-17 04:20:05

+0

您是否通過在樂器程序中選擇「創建並保持生活」來檢查?對不起,只有2個月的編碼,所以我仍然在學習 – bbullis21 2009-09-17 04:32:01

-1

我編譯並運行你的代碼,因爲你有它,我沒有看到任何泄漏,我的對象alloc也沒有繼續攀升。我每秒運行代碼幾次,並沒有看到儀器中有任何對象增長。我只在模擬器上運行。我還嘗試了kCGInterpolationNone而不是3.0,以防止問題發生,並且仍然沒有泄漏。

不知道爲什麼我沒有得到他們,你這樣做。你可能想只是這樣做的方法:

-(UIImage *) resizedImage:(UIImage *)inImage : (CGRect)thumbRect : (double)interpolationQuality 
{ 
    CGImageRef     imageRef = [inImage CGImage]; 
    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); 

    return inImage; 
... 

爲了使這種方法是沒有意義的,然後觀察,看是否該對象的alloc繼續增長。如果是這樣,那麼問題在別處。

+0

我給了這個嘗試,結果仍然相同。 ObjectAlloc構建仍然指向CGBitmapContextCreateImage調用。不過謝謝。連續第三天,我一直把頭髮拉出來。 – bbullis21 2009-09-17 03:15:14

+0

試試這個:而不是提供NULL到CGBitmapCreateContext,傳給它bmapPtr = malloc(height * 4 * thumbRect.size.width);不要忘記在方法結尾處釋放(bmapPtr)。 – mahboudz 2009-09-17 03:28:31

+0

是bmapPtr一個int?我不知道該怎麼聲明它? – bbullis21 2009-09-17 03:45:40