gpt4 book ai didi

ios - 在 iPhone 上将照片保存到磁盘的最有效内存方式?

转载 作者:技术小花猫 更新时间:2023-10-29 10:17:41 25 4
gpt4 key购买 nike

通过使用 Instruments 进行分析,我了解到我将图像保存到磁盘的方式导致内存峰值达到 ~60MB。这会导致应用程序发出低内存警告,这(不一致地)导致运行 iOS7iPhone4S 崩溃。

我需要以最有效的方式将图像保存到磁盘。

我目前正在使用这个代码

+ (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];
}

注意事项:

  1. 减少UIImageJPEGRepresentationcompressionQuality 参数的值并不能显着减少内存峰值。例如compressionQuality = 0.8,在 100 写入中平均将内存峰值减少了 3MB。但是,它确实减少了磁盘上数据的大小(很明显),但这对我没有帮助。

  2. UIImagePNGRepresentation 代替 UIImageJPEGRepresentation 对此更糟。它速度较慢并导致更高的尖峰。

有没有可能 this approach使用 ImageIO 会更有效率吗?如果是,为什么?

如果有人有任何建议,那就太好了。谢谢

编辑:

关于以下问题中概述的一些要点的注释。

a) 虽然我保存了多张图片,但我并没有循环保存它们。我做了一些阅读和测试,发现自动释放池对我没有帮助。

b) 照片的大小不是每张 60Mb。它们是在 iPhone 4S 上拍摄的照片。

考虑到这一点,我回去尝试克服我认为的问题所在; NSData *data = UIImageJPEGRepresentation(image, 1.0); 行。

导致崩溃的内存峰值可以在下面的屏幕截图中看到。它们对应于调用 UIImageJPEGRepresentation 的时间。我还运行了 Time ProfilerSystem Usage,这为我指明了相同的方向。

enter image description here

长话短说,我转到了 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 上将照片保存到磁盘的最有效内存方式?”。但是,如果达成共识,我可以更新它。

谢谢。

最佳答案

使用 UIImageJPEGRepresentation 要求您在内存中同时拥有原始图像和最终图像。它还可能会缓存完全渲染的图像一段时间,这会使用大量内存。

您可以尝试使用 CGImageDestination .我不知道它的内存效率如何,但它有可能将图像直接流式传输到磁盘。

+(void) writeImage:(UIImage *)inImage toURL:(NSURL *)inURL withQuality:(double)inQuality {
CGImageDestinationRef destination = CGImageDestinationCreateWithURL( (CFURLRef)inURL , kUTTypeJPEG , 1 , NULL );
CFDictionaryRef properties = (CFDictionaryRef)[NSDictionary dictionaryWithObject:[NSNumber numberWithDouble:inQuality] forKey:kCGImageDestinationLossyCompressionQuality];
CGImageDestinationAddImage( destination , [inImage CGImage] , properties );
CGImageDestinationFinalize( destination );
CFRelease( destination );
}

关于ios - 在 iPhone 上将照片保存到磁盘的最有效内存方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20670193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com