我通过线程从URL接收图像。 但内存泄漏在NSData中。 为什么?我如何解决它? 在Iphone devie中泄漏而不是在模拟器中。帮助我!!
//的ViewController
-(void)viewDidLoad
{
[self performSelectorInBackground:@selector(loadImageScreenshot:) withObject:args];
}
// loding image
-(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];
}
// image add
- (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;
}
答案 0 :(得分:1)
你怎么知道它在漏水?您正在- (void) assignImageToScreenshotImageView:(NSDictionary *)arg
创建一个永不消耗的自动释放池,这是一个泄漏。
否则代码似乎没问题。