显示大图像文件时在 viewDidAppear 之后调用 DidRecieveMemoryWarning [英] DidRecieveMemoryWarning called after viewDidAppear when displaying large image file

查看:80
本文介绍了显示大图像文件时在 viewDidAppear 之后调用 DidRecieveMemoryWarning的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我加载特定页面时,由于内存压力,我的应用程序已终止.我正在使用 NSURLRequest 来获取数据并加载图像.我在整个代码中放置了断点,并在 viewDidAppear 之后跟踪第一个 didRecieveMemoryWarning 调用.这是我用来加载图像的代码:

My app has terminated due to memory pressure when I load a particular page. I am using an NSURLRequest to get data and load an image. I placed breakpoints throughout my code and traced the first didRecieveMemoryWarning call to right after viewDidAppear. Here is the code I am using to load the image:

NSError *error = nil;
NSURLResponse *response = nil;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.img]];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
UIImage *img = [[UIImage alloc] initWithData:data];
self.ImgView.image = img;
[self.scrollView addSubview:self.ImgView];

关于我收集的数据的一些事实:

Some facts about the data that I have gathered:

  1. 在线图像为 5,184 像素 × 3,456 像素(缩放至 1,008 像素 × 672 像素)
  2. 我将图像放入 320 x 320 UIImageView
  3. NSData 对象数据为 5,360,785 字节

我该如何解决这个问题?是应用程序无法处理数据,还是 imageView 无法缩放图像?图像显示在屏幕上,但无论您是停留在该页面上还是离开该页面,应用在屏幕加载后都会崩溃.

How can I correct this problem? Is the app just not able to handle the data, or is the imageView not able to scale the image? The image appears onscreen, but the app crashes a bit after the screen loads, whether you stay on that page or leave it.

推荐答案

在视图加载后尝试加载图像 &在分配给 imageview 之前,稍微缩小图像.这避免了内存警告.

Try to load the image once the view is loaded & before assigning to imageview scale down the image little bit. This avoid memory warning.

参考:NSData 到 UIImage

用于调整图像大小:如何缩小 UIImage 并使其同时清晰/清晰而不是模糊?

这篇关于显示大图像文件时在 viewDidAppear 之后调用 DidRecieveMemoryWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆