2011-05-04 59 views
3

現在無休止的時間一直在尋找解決這個問題的方法。問題很簡單。我在筆尖有一個UIImageView,我正在從網上加載一張圖片,並將圖片設置爲UIIImageView。我釋放viewController。 Dealloc實際上正在調用(!)並再次加載viewController和圖像。這可以通過非常容易地進入背景模式來完成。儀器泄漏不報告任何泄漏,但它表明它在分配中保持圖像在內存中並且保持增長。UIImageView setImage泄漏?

基本例如:

-(id)init { 
    if((self = [super init])) { 
     id path = [NSString stringWithFormat:@"http://www.aSite.com/largeImage.jpg"]; 
     NSData* urlData = [[NSData alloc] initWithContentsOfURL:url]; 
     UIImage* img = [[UIImage alloc] initWithData:urlData]; 
     [background setImage:img]; 
     [urlData release]; 
     [img release]; 
    } 
    return self; 
} 

-(void)dealloc { 
    [background release]; 
    [super dealloc]; 
} 

有人說實際上的UIImageView泄漏,或實際CGImage。有些人說樂器顯示不正確。我在用2.5mb大圖片做10-15次後得到了內存警告。結果來自實際設備和最新的iOS(或至少4-5周前)。由於UIImageView被很多人使用,我認爲它很容易找到問題或從蘋果獲得修復?

來源CGImage泄漏的: (iphone) UIImageView setImage: leaks?

編輯:我是升技tierd當我寫的例子。示例現在正確。我也嘗試過使用自動釋放對象,同樣的「泄漏」仍然存在。如果你要寫答案,請回答問題

回答

1

在下面的代碼中,你犯了一些錯誤。

[urlData release]; 
    [background setImage:[UIImage imageWithData:urlData]]; 

你應該使用

if((self = [super init])) { 
     id path = [NSString stringWithFormat:@"http://www.aSite.com/largeImage.jpg"]; 
     NSData* urlData = [[NSData alloc] initWithContentsOfURL:url]; 
     UIImage* img = [[UIImage alloc] initWithData:urlData]; 
     [background setImage:img]; 
     [urlData release]; 
     [img release]; 
    } 
+0

與問題無關。另外[[UIImage alloc] initWithData:urlData]保留了NSData,因此在[backgroundsetImage:img]之前釋放它是可以的...... – David 2011-05-04 05:31:41

0

不能更換

[background setImage:[UIImage imageWithData:urlData]]; 

[background setImage:img]; 

UPDATE

我想,這也應該幫助

if((self = [super init])) { 
     id path = [NSString stringWithFormat:@"http://www.aSite.com/largeImage.jpg"]; 
     NSData* urlData = [[NSData alloc] initWithContentsOfURL:url]; 
     [background setImage:[UIImage imageWithData:urlData]]; 
     [urlData release]; 

    } 
+0

我嘗試過使用自動釋放對象的相同內存「leaks」 – David 2011-05-04 05:14:15

+0

那是隻是一個選項,你嘗試了第一個。 – visakh7 2011-05-04 05:14:45

+0

是的,同樣的問題。 – David 2011-05-04 05:19:11

2

release的URL設置圖像後

[background setImage:[UIImage imageWithData:urlData]]; 
[urlData release]; 
[img release]; 
+0

與問題無關。另外[[UIImage alloc] initWithData:urlData]保留了NSData,因此在[background setImage:img]之前釋放它是個好主意........ – David 2011-05-04 05:31:33

0

你試過:

-(id)init { 
    if((self = [super init])) 
    { 

     [background setImage: 
      [UIImage imageWithData: 
       [NSData dataWithUrl: 
        [NSUrl urlWithString: 
         @"http://www.aSite.com/largeImage.jpg" ]]] 
     ]; 

    } 
    return self; 
} 

-(void)dealloc { 
    [super dealloc]; 
} 

乾淨,沒有內存泄漏!