UIGraphicsGetImageFromCurrentImageContext 缩放图像时内存泄漏

发布于 2024-09-06 02:58:28 字数 1407 浏览 1 评论 0原文

我的 iPhone 应用程序从服务器下载图像文件,将其存储到 NSTemporaryDirectory() 中,然后在 UI 中异步加载图像。代码流程如下:

  1. 显示带有加载活动指示器的视图并在后台运行图像下载器。
  2. 下载图像后,会将其写入文件。
  3. 加载视图中的计时器不断检查临时目录中文件的可用性,一旦可用,就会从文件加载图像并将图像添加到 UI。
  4. 在添加图像之前,将其缩放到所需的尺寸。

问题是,我使用 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];

我是什么这里不见了?

My iPhone application downloads image files from a server, stores it into NSTemporaryDirectory() and then loads the image in the UI asynchronously. Code flow is like this:

  1. Show view with loading activity indicator and run a image downloader in the background.
  2. Once the image is downloaded, it is written to a file.
  3. A timer in the loading view keep checking for the availability of file in the temp directory and once available, loads the image from file and adds the image to the UI.
  4. Before adding the image, it is scaled to required size.

Problem is, I use UIGraphicsGetImageFromCurrentImageContext to scale the image. Looks like the memory used by the image context is not getting cleaned. The app memory just keeps increasing as more files get downloaded.

Some code below:

Code to scale the image:


-(UIImage*)scaleToSize:(CGSize)size image:(UIImage *)imageref
{
 UIGraphicsBeginImageContext(size);
 [imageref drawInRect:CGRectMake(0, 0, size.width, size.height)];
 UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 return scaledImage;
}

Loading image from temp directory:


-(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];
}

Adding image to view (subset of code):


 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];

What am I missing here ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑咖 2024-09-13 02:58:28

避免 UIGraphicsGetImageFromCurrentImageContext 泄漏的一种方法是通过调整容器大小而不是直接调整图像大小来根本不调用它:

self.imageContainer.contentMode = UIViewContentModeScaleAspectFit;
self.imageContainer.frame = CGRectMake(self.imageContainer.frame.origin.x, self.imageContainer.frame.origin.y, 320.0f, 250.0f);

One way of avoiding the leak from UIGraphicsGetImageFromCurrentImageContext is to not call it at all by resizing the container instead of resizing image directly:

self.imageContainer.contentMode = UIViewContentModeScaleAspectFit;
self.imageContainer.frame = CGRectMake(self.imageContainer.frame.origin.x, self.imageContainer.frame.origin.y, 320.0f, 250.0f);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文