问题描述
使用UIImageJPEGRepresentation时收到内存警告,有什么方法可以避免这种情况吗?它不会使应用程序崩溃,但我希望尽可能避免使用它。它间歇性地不运行 [[UIApplication sharedApplication] openURL:url];
I receive a memory warning when using UIImageJPEGRepresentation, is there any way to avoid this? It doesn't crash the app but I'd like to avoid it if possible. It does intermittently not run the [[UIApplication sharedApplication] openURL:url];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
NSData *imageToUpload = UIImageJPEGRepresentation(image, 1.0);
// code that sends the image to a web service (omitted)
// on success from the service
// this sometime does not get run, I assume it has to do with the memory warning?
[[UIApplication sharedApplication] openURL:url];
}
推荐答案
使用 UIImageJPEGRepresentation
(通过 UIImage
对资产进行往返)可能会有问题,因为使用 compressionQuality
为1.0,结果 NSData
实际上可能比原始文件大得多。 (另外,你在 UIImage
中持有第二张图像副本。)
Using UIImageJPEGRepresentation
(in which you are round-tripping the asset through a UIImage
) can be problematic, because using a compressionQuality
of 1.0, the resulting NSData
can actually be considerably larger than the original file. (Plus, you're holding a second copy of the image in the UIImage
.)
例如,我刚刚从iPhone的照片库中选择了一张随机图片,原始资产为1.5mb,但 NSData
由 UIImageJPEGRepresentation
生成使用 compressionQuality
1.0需要6.2mb。并且在 UIImage
中保存图像本身可能需要更多内存(因为如果未压缩,则每个像素可能需要四个字节)。
For example, I just picked a random image from my iPhone's photo library and the original asset was 1.5mb, but the NSData
produced by UIImageJPEGRepresentation
with a compressionQuality
of 1.0 required 6.2mb. And holding the image in UIImage
, itself, might take even more memory (because if uncompressed, it can require, for example, four bytes per pixel).
相反,您可以使用 getBytes
方法获取原始资产:
Instead, you can get the original asset using the getBytes
method:
static NSInteger kBufferSize = 1024 * 10;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *url = info[UIImagePickerControllerReferenceURL];
[self.library assetForURL:url resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *representation = [asset defaultRepresentation];
long long remaining = representation.size;
NSString *filename = representation.filename;
long long representationOffset = 0ll;
NSError *error;
NSMutableData *data = [NSMutableData data];
uint8_t buffer[kBufferSize];
while (remaining > 0ll) {
NSInteger bytesRetrieved = [representation getBytes:buffer fromOffset:representationOffset length:sizeof(buffer) error:&error];
if (bytesRetrieved <= 0) {
NSLog(@"failed getBytes: %@", error);
return;
} else {
remaining -= bytesRetrieved;
representationOffset += bytesRetrieved;
[data appendBytes:buffer length:bytesRetrieved];
}
}
// you can now use the `NSData`
} failureBlock:^(NSError *error) {
NSLog(@"assetForURL error = %@", error);
}];
}
这可以避免在 UIImage中暂存图像
和结果 NSData
可以(对于照片,无论如何)相当小。注意,这也有一个优点,它也保留了与图像相关的元数据。
This avoids staging the image in a UIImage
and the resulting NSData
can be (for photos, anyway) considerably smaller. Note, this also has an advantage that it preserves the meta data associated with the image, too.
顺便说一句,虽然上面代表了显着的内存改进,但你可以可能会看到一个更显着的内存减少机会:具体来说,您不必一次将整个资产加载到 NSData
,您现在可以流式传输资产(子类 NSInputStream
使用此 getBytes
例程来获取需要的字节,而不是一次将整个内容加载到内存中。这个过程涉及一些烦恼(参见 BJ Homer关于该主题的文章),但如果你正在寻找显着减少内存占用,那就是这样。这里有几种方法(BJ,使用一些暂存文件和流媒体等),但关键是流式传输可以大大减少你的内存占用。
By the way, while the above represents a significant memory improvement, you can probably see a more dramatic memory reduction opportunity: Specifically, rather than loading the entire asset into a NSData
at one time, you can now stream the asset (subclass NSInputStream
to use this getBytes
routine to fetch bytes as they're needed, rather than loading the whole thing into memory at one time). There are some annoyances involved with this process (see BJ Homer's article on the topic), but if you're looking for dramatic reduction in the memory footprint, that's the way. There are a couple of approaches here (BJ's, using some staging file and streaming from that, etc.), but the key is that streaming can dramatically reduce your memory footprint.
但是在 UIImageJPEGRepresentation
中避免 UIImage
(这会避免图像占用的内存以及较大的 NSData
UIImageJPEGRepresentation
收益率),您可能会取得相当大的进展。此外,您可能希望确保一次不在内存中存储此图像数据的冗余副本(例如,不要将图像数据加载到 NSData
,然后为 HTTPBody
构建第二个 NSData
...看看你是否可以一举做到这一点) 。如果情况变得更糟,你可以采用流媒体方式。
But by avoiding UIImage
in UIImageJPEGRepresentation
(which avoids the memory taken up by the image as well as the larger NSData
that UIImageJPEGRepresentation
yields), you might be able to make considerably headway. Also, you might want to make sure that you don't have redundant copies of this image data in memory at one time (e.g. don't load the image data into a NSData
, and then build a second NSData
for the HTTPBody
... see if you can do it in one fell swoop). And if worst comes to worse, you can pursue streaming approaches.
这篇关于UIImageJPEGRepresentation收到内存警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!