Memory leaks 用于缩放图像的UIGraphicsGetImageFromCurrentImageContext内存泄漏
我的iPhone应用程序从服务器下载图像文件,将其存储到NSTemporaryDirectory()中,然后在UI中异步加载图像。代码流如下所示:Memory leaks 用于缩放图像的UIGraphicsGetImageFromCurrentImageContext内存泄漏,memory-leaks,uikit,image-manipulation,Memory Leaks,Uikit,Image Manipulation,我的iPhone应用程序从服务器下载图像文件,将其存储到NSTemporaryDirectory()中,然后在UI中异步加载图像。代码流如下所示: 显示带有加载活动指示器的视图,并在后台运行图像下载程序 图像下载后,将写入文件 加载视图中的计时器会不断检查临时目录中文件的可用性,一旦可用,就会从文件中加载图像并将图像添加到UI中 添加图像之前,将其缩放到所需大小 问题是,我使用UIGraphicsGetImageFromCurrentImageContext来缩放图像。图像上下文使用的内存似乎未
-(UIImage*)scaleToSize:(CGSize)size image:(UIImage *)imageref
{
UIGraphicsBeginImageContext(size);
[imageref drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
-(void)loadImageFromFile: (NSString *) path
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
UIImage * imm = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
[self performSelectorOnMainThread:@selector(insertImage:) withObject:imm waitUntilDone:YES];
[pool release];
}
self.imageContainer = [[UIImageView alloc] initWithFrame:CGRectMake(0,80,320,250)];
[self addSubview:self.imageContainer];
self.imageContainer.image = [self scaleToSize:CGSizeMake(320.0f, 250.0f) image:imm];
[imageContainer release];
从临时目录加载图像:
-(UIImage*)scaleToSize:(CGSize)size image:(UIImage *)imageref
{
UIGraphicsBeginImageContext(size);
[imageref drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
-(void)loadImageFromFile: (NSString *) path
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
UIImage * imm = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
[self performSelectorOnMainThread:@selector(insertImage:) withObject:imm waitUntilDone:YES];
[pool release];
}
self.imageContainer = [[UIImageView alloc] initWithFrame:CGRectMake(0,80,320,250)];
[self addSubview:self.imageContainer];
self.imageContainer.image = [self scaleToSize:CGSizeMake(320.0f, 250.0f) image:imm];
[imageContainer release];
将图像添加到视图(代码子集):
-(UIImage*)scaleToSize:(CGSize)size image:(UIImage *)imageref
{
UIGraphicsBeginImageContext(size);
[imageref drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
-(void)loadImageFromFile: (NSString *) path
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
UIImage * imm = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
[self performSelectorOnMainThread:@selector(insertImage:) withObject:imm waitUntilDone:YES];
[pool release];
}
self.imageContainer = [[UIImageView alloc] initWithFrame:CGRectMake(0,80,320,250)];
[self addSubview:self.imageContainer];
self.imageContainer.image = [self scaleToSize:CGSizeMake(320.0f, 250.0f) image:imm];
[imageContainer release];
我在这里遗漏了什么?避免从
UIGraphicsGetImageFromCurrentImageContext
泄漏的一种方法是根本不通过调整容器大小而不是直接调整图像大小来调用它:
self.imageContainer.contentMode = UIViewContentModeScaleAspectFit;
self.imageContainer.frame = CGRectMake(self.imageContainer.frame.origin.x, self.imageContainer.frame.origin.y, 320.0f, 250.0f);
是否也删除子视图?你认为你泄漏了多少内存?也许网络加载是你的问题?是什么让你认为缩放是你的问题?谢谢你的回答。进一步的调查表明,缩放不是真正的问题。没有清理的是UI映像实例。有没有线索说明为什么会这样?你有没有找到解决办法?我当前遇到了相同的问题:
UIImage
使用UIGraphicsGetImageFromCurrentImageContext()
创建的UIImage正在泄漏内存。