2012-03-24 66 views
2

我通過線程接收來自URL的圖像。 但內存泄漏在NSData中。 爲什麼?我如何解決它? 泄漏在Iphone devie不在模擬器中。幫我!!內存泄漏使用URL中的NSData

//視圖 - 控制

-(void)viewDidLoad 
{ 

[self performSelectorInBackground:@selector(loadImageScreenshot:) withObject:args]; 

} 

//裝載儀圖像

-(void) loadImageScreenshot:(NSDictionary *) args 
{ 
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
UIImage * screenshotImage=[UIImage imageWithStringURL:url]; 
NSDictionary *args2=[NSDictionary dictionaryWithObjectsAndKeys: 
        [NSNumber numberWithInt:num], @"screenNum", 
        screenshotImage,@"image", 
        nil];                

[self performSelectorOnMainThread:@selector(assignImageToScreenshotImageView:) withObject:args2 waitUntilDone:YES]; 

[pool release]; 

} 

//圖像從URL添加

- (void) assignImageToScreenshotImageView:(NSDictionary *)arg 

{ 

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
UIImage * image= [arg objectForKey:@"image"]; 
UIImageView *imageview=[UIImageView alloc]init]; 
       . 
       . 
imageview.image=image; 
[self.mScreenshotSpace addSubview:imageview]; 
[imageview release]; 
[pool release]; 
} 

//圖像

+(UIImage *)imageWithStringURL:(NSString *)strURL 
{ 
NSURL *url =[NSURL URLWithString:strURL]; 
NSData * data=[[NSData alloc]initWithContentsOfURL:url options:NSDataReadingUncached error:&error]; 

UIImage * image=[UIImage imageWithData:data ]; 
[data release]; 
return image; 
} 

回答

1

你怎麼知道它在泄漏?您正在- (void) assignImageToScreenshotImageView:(NSDictionary *)arg中創建一個永不耗盡的自動釋放池,即泄漏。 否則代碼似乎很好。

+0

In使用儀器,泄漏內存。 - (void)assignImageToScreenshotImageView:(NSDictionary *)arg是問題嗎?我如何解決它? – SeoTaiJi 2012-03-24 12:37:35

+1

現在你已經添加了'[pool release]'行,對我來說看起來很好。 – Tark 2012-03-24 13:59:30