我正在尝试复制图像UIImagePicker
到文档目录。我在用@"UIImagePickerControllerOriginalImage"
key 从 UIImagePickerDelegate 的字典中获取原始图像。我正在使用将图像写入文件UIImagePNGRepresentation
。当我添加(重复该过程)高分辨率图像(图像大小约为 20 mb)时,我面临内存问题。
我分析并使用了 Xcode 的内存泄漏功能,它放大了下面一段代码,该代码是造成泄漏的原因。
@autoreleasepool {
imagesData = UIImagePNGRepresentation(images);
[imagesData writeToFile:name atomically:NO];
imagesData = nil;
//[UIImageJPEGRepresentation(images, 1.0) writeToFile:name atomically:YES];
}
我在这里看到了很多关于内存泄漏的问题UIImagePNGRepresentation
。但我还没有找到解决我的问题的正确方法。需要帮忙。
我不知道有任何“泄漏”UIImagePNGRepresentation
,但这确实是对内存的过度使用,但这里有几个问题:
-
首先,通过a往返原始资产的过程UIImage
然后使用UIImagePNGRepresentation()
效率相当低,最终可能会得到NSData
这比原始资产大得多。例如,我选择了一张原始资源为1.5mb的照片,UIImageJPEGRepresentation
(with compressionQuality
为 1.0) 为 6mb,并且UIImagePNGRepresentation()
大约10mb。 (这些数字可能因图像而异,但您已经了解了基本概念。)
您通常可以通过使用来缓解此问题UIImageJPEGRepresentation
with a compressionQuality
小于 1.0(例如 0.8 或 0.9 提供最小的图像质量损失,但仍可观察到NSData
地点)。但这是有损压缩。此外,在此过程中您会丢失一些图像元数据。
-
我相信您同时在内存中保存了同一图像的多个副本:您同时拥有UIImage
代表以及NSData
object.
-
不仅是NSData
如果资产的表示大于所需的大小,您还会将整个资产一次性加载到内存中。这是没有必要的。
相反,您可能会考虑从流式传输原始资源ALAssetLibrary
直接写入持久内存,无需使用UIImagePNGRepresentation
or UIImageJPEGRepresentation
,并且无需将其加载到UIImage
根本不。相反,创建一个小缓冲区,通过以下方式重复用原始资源的部分填充此缓冲区getBytes
,使用以下命令将这个小缓冲区写入临时文件NSOutputStream
当你继续前进时。您可以重复该过程,直到将整个资产写入持久存储。该过程的总内存占用量比其他方法低得多。
例如:
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;
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *path = [documentsPath stringByAppendingPathComponent:filename];
NSString *tempPath = [self pathForTemporaryFileWithPrefix:@"ALAssetDownload"];
NSOutputStream *outputStream = [NSOutputStream outputStreamToFileAtPath:tempPath append:NO];
NSAssert(outputStream, @"Unable to create output stream");
[outputStream open];
long long representationOffset = 0ll;
NSError *error;
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);
[outputStream close];
[[NSFileManager defaultManager] removeItemAtPath:tempPath error:nil];
return;
} else {
remaining -= bytesRetrieved;
representationOffset += bytesRetrieved;
[outputStream write:buffer maxLength:bytesRetrieved];
}
}
[outputStream close];
if (![[NSFileManager defaultManager] moveItemAtPath:tempPath toPath:path error:&error]) {
NSLog(@"Unable to move file: %@", error);
}
} failureBlock:^(NSError *error) {
NSLog(@"assetForURL error = %@", error);
}];
}
- (NSString *)pathForTemporaryFileWithPrefix:(NSString *)prefix
{
NSString *uuidString = [[NSUUID UUID] UUIDString];
// If supporting iOS versions prior to 6.0, you can use:
//
// CFUUIDRef uuid = CFUUIDCreate(NULL);
// assert(uuid != NULL);
// NSString *uuidString = CFBridgingRelease(CFUUIDCreateString(NULL, uuid));
// CFRelease(uuid);
return [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-%@", prefix, uuidString]];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)