2010-11-05 55 views
0

我有下面的代碼從網絡加載圖像。 單擊表格單元格後會顯示這些圖像,並且每次單擊表格單元格時都會重新載入這些圖像。 關鍵是用「工具」分析內存分配,當我從詳細視圖返回到表格時,圖像佔用的內存不會被釋放。iPhone:圖像使用的內存未被釋放

有沒有人有任何建議? 我當然會盡全力發佈案件......但似乎沒有幫助。

NSError *error = nil; 
    UIImage *img = nil; 
    NSString *myurl = [IMG_SERVER_URL stringByAppendingString: tmp_link.url]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    NSData *imageData = nil; 
    if(myurl!=nil){ 
     imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:myurl] options:nil error:&error]; 
     if (error == 0) { 
      img = [[UIImage alloc] initWithData:imageData]; 
     } 
    } 
    [imageData release]; imageData = nil; 
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(targetWidth*i, 0, targetWidth, targetHeight)]; 
    imageView.image = img; 
    imageView.contentMode = UIViewContentModeScaleAspectFit; 
    [img release]; 
    [....some code....] 
    [imageView release]; 
+1

img在哪裏發佈? – deanWombourne 2010-11-05 12:23:45

+0

我已經添加了幾行代碼,這很有意義,謝謝你發現了一個缺失的有用部分。 – 0m4r 2010-11-05 12:46:00

回答

2

我認爲「imageView.image = img」增加了圖像對象的引用計數。如果我是對的,只要你不釋放imageView,分配的內存就不會被釋放。

+0

我在代碼中稍後發佈imageView ....我已經更新了代碼片斷,並且非常感謝您的建議! – 0m4r 2010-11-05 13:04:34