大多数内存有效的方法将照片保存到iPhone上的磁盘?

 mobiledu2502887493 发布于 2023-02-09 12:50

从剖析中Instruments我了解到,我将图像保存到磁盘的方式导致了内存峰值~60MB.这导致应用程序发出low memory warnings,(不一致)导致iPhone4S运行中的崩溃iOS7.

我需要最有效的方法将图像保存到磁盘.

我目前正在使用此代码

+ (void)saveImage:(UIImage *)image withName:(NSString *)name {
    NSData *data = UIImageJPEGRepresentation(image, 1.0);
    DLog(@"*** SIZE *** : Saving file of size %lu", (unsigned long)[data length]);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
    [fileManager createFileAtPath:fullPath contents:data attributes:nil];
}

笔记:

    减少compressionQuality参数值UIImageJPEGRepresentation不会显着降低内存峰值.例如 compressionQuality = 0.8,3MB通过100写入平均减少内存峰值.但是,它确实减少了磁盘上数据的大小(显然),但这对我没有帮助.

    UIImagePNGRepresentation取而代之的UIImageJPEGRepresentation是更糟糕的.速度较慢,导致峰值较高.

难道这种做法有ImageIO会更有效?如果是这样的话?

如果有人有任何建议,那将是伟大的.谢谢

编辑:

关于以下问题中概述的一些要点的说明.

a)虽然我正在保存多个图像,但我没有将它们保存在循环中.我做了一些阅读和测试,发现自动释放池不会帮助我.

b)每张照片的尺寸不是60Mb.他们是在iPhone 4S上拍摄的照片.

考虑到这一点,我回过头来试图克服我认为的问题; 这条线NSData *data = UIImageJPEGRepresentation(image, 1.0);.

导致崩溃的内存峰值可以在下面的屏幕截图中看到.他们对应于UIImageJPEGRepresentation被召唤的时间.我也跑了Time Profiler,System Usage它指向了同一个方向.

在此输入图像描述

长话短说,我转过来AVFoundation拍摄照片图像数据

photoData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];

返回一个类型的对象NSData,然后我使用它作为使用NSFileManager写入的数据.

这样可以完全消除内存中的尖峰.

[self saveImageWithData:photoData];

哪里

+ (void)saveImageWithData:(NSData *)imageData withName:(NSString *)name {
    NSData *data = imageData;
    DLog(@"*** SIZE *** : Saving file of size %lu", (unsigned long)[data length]);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
    [fileManager createFileAtPath:fullPath contents:data attributes:nil];
}

PS:我没有把这个作为问题的答案,因为人们觉得它没有回答标题"在iPhone上将照片保存到磁盘的最有效的内存方式吗?".但是,如果达成共识的话应该是我可以更新它.

谢谢.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有