处理 iPhone 中的内存不足错误
我想处理 iPhone 中的内存不足错误,以便在内存不足的情况下以较少的内存需求执行逻辑。特别是,我想做一些与以下伪代码非常相似的事情:
UIImage* image;
try {
image = [UIImage imageNamed:@"high_quality_image.png"];
} catch (OutOfMemoryException e) {
image = [UIImage imageNamed:@"low_quality_image.jpg"];
}
首先,我尝试加载高质量的图像,如果在执行此操作时内存不足,那么我会使用较低质量的图像。
这可能吗?发生内存不足错误时是否存在某种无法处理的异常或通知?
内存不足警告不是我想要的,因为它是在手机内存不足之前收到的。我想知道我正在执行的逻辑是否由于内存不足错误而失败,并相应地处理这个问题。
或者,类似这样的事情也可以有所帮助:
UIImage* image;
if (enoughMemory) {
image = [UIImage imageNamed:@"high_quality_image.png"];
} else {
image = [UIImage imageNamed:@"low_quality_image.jpg"];
}
I would like to handle out of memory errors in iPhone to execute logic with lesser memory requirements in case I run of of memory. In particular, I would like to do something very similar to the following pseudo-code:
UIImage* image;
try {
image = [UIImage imageNamed:@"high_quality_image.png"];
} catch (OutOfMemoryException e) {
image = [UIImage imageNamed:@"low_quality_image.jpg"];
}
First I attempt to load a high-quality image, and if I run out of memory while doing it, then I use a lower quality image.
Would this be possible? Is there some kind of exception or notification than can be handled when an out of memory error occurs?
The out of memory warning is not what I'm looking for, as it's received before the phone runs out of memory. I would like to know if the logic I'm executing failed because an out of memory error, and deal with this accordingly.
Alternatively, something like this could also help:
UIImage* image;
if (enoughMemory) {
image = [UIImage imageNamed:@"high_quality_image.png"];
} else {
image = [UIImage imageNamed:@"low_quality_image.jpg"];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个答案,可以让您了解应用程序当前使用的内存,您可以根据该信息决定要做什么。
iPhone 内存使用情况
here is an answer that will get you the current memory used by your app and you can decide what to do based on that info.
iphone memory usuage
iPhone 具有内存不足警告,当达到特定内存阈值时会触发该警告。此回调被发送到应用程序,并且必须做出适当响应,否则您的应用程序将面临被操作系统抢占的风险。那时,您的应用程序最好将高内存图像转换为低内存图像。
不过,这里要小心,因为磁盘上文件的大小不一定代表文件加载并准备在设备上使用后将消耗多少内存。
请参阅 此处了解有关处理内存不足警告的更多信息。
The iPhone has a low memory warning that is triggered when a certain memory threshold is reached. This callback is issued to the application and must be responded to appropriately or your application runs the risk of being preempted by the OS. At that time, then, it would be good for your application to convert the high-memory images to more low memory ones.
Be careful here, though, as the size of the file on disk is not necessarily representative of how much memory it will consume once it has been loaded and prepped for use on the device.
See here for more information on handling low memory warnings.
编辑:看起来这并不是您问题的答案。
您想对您的图像做什么?某些操作(例如存储从图像选择器控制器返回的全尺寸图像)会占用大量内存。
我建议做的是始终假设您始终需要最小尺寸的图像,并且:
现在,这种情况可能不适合你。在 iPhone 上读取和写入大图像到磁盘的速度很慢。如果是这种情况,您要做的就是遵循 @fbrereto 的建议:加载大图像,注意内存警告,如果收到警告,请将其删除并加载小图像。
您绝对不应该忽略内存警告,但不响应警告并不一定会使您的应用程序崩溃。几乎每次我从
UIImagePickerController
获取图像时,我的 iPhone 3G 都会收到内存警告,但我对此无能为力。Edit: it looks like this wasn't quite the answer to your question.
What are you trying to do with your images? Some actions, like storing a full-sized image returned from the image picker controller are particularly heavy memory users.
What I would recommend doing is always assuming you need the smallest sized images at all times and:
Now, it's possible that this situation won't work for you. Reading and writing large images to disk on the iPhone is slow. If that's the case what you'll have to do is follow @fbrereto's suggestion: load the large image, watch out for memory warnings and if you get one, drop it and load the small image.
You definitely shouldn't ignore memory warnings, but not responding to one won't necessarily crash your app. My iPhone 3G will get memory warnings almost every time I get an image back from the
UIImagePickerController
, and there's nothing I can do about it.