【发布时间】:2013-12-13 05:33:16
【问题描述】:
我正在尝试解决这个问题,但在完成我在操作系统或谷歌上找到的每一件事后都失败了。问题是当我使用UIImageJPEGRepresentation 或UIImagePNGRepresentation 将UIImage 转换为NSData 时,它会将内存大小增加到30Mb(信不信由你)。
这是我的代码
myImage= image;
LoginSinglton*loginObj = [LoginSinglton sharedInstance];
NSError *error;
NSData *pngData = UIImageJPEGRepresentation(image, scaleValue); //scaleVale is 1.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
self.imageCurrentDateAndTime =[self getTimeAndDate];
self.filePathAndDirectory = [documentsPath stringByAppendingPathComponent:@"Photos Dir"];
NSLog(@"Documents path %@",self.filePathAndDirectory);
if (![[NSFileManager defaultManager] createDirectoryAtPath:self.filePathAndDirectory
withIntermediateDirectories:NO
attributes:nil
error:&error])
{
NSLog(@"Create directory error: %@", error);
}
self.imageName= [NSString stringWithFormat:@"photo-%@-%@.jpg",loginObj.userWebID,self.imageCurrentDateAndTime];
NSString *filePath = [self.filePathAndDirectory stringByAppendingPathComponent:self.imageName];
[pngData writeToFile:filePath atomically:YES]; //Write the file
[self writeImageThumbnailToFolder:image];
[self writeImageHomeViewThumbnailToFolder:image];
我也尝试过以下解决方案
UIImageJPEGRepresentation - memory release issue
1- 使用 @autoreleasepool
2- 完成 pngData = nil;
但仍然面临着内存问题。
编辑我想我无法表达我的问题。如果UIImageJPEGRepresentation 占用大量内存也没关系,但在保存该图像后内存应该恢复到之前的位置。希望这会对您有所帮助。
【问题讨论】:
-
你图片的分辨率是多少?让我猜猜... 8 兆像素,即 3264 x 2448 或 3264 x 2448 x 4 字节,每个加载到内存中的位图大约 30 Mo... 也许这就是您的问题,如果按比例缩小压缩还不够,请调整大小?
-
当然,您最终会得到大量数据。您正在拍摄压缩图像并将其解压缩为纯数据表示,这将是巨大的。
-
@Vinzzz 我不确定大小,但分辨率接近你提到的,因为我使用的是“AVCaptureSessionPresetPhoto”
-
@MZimmerman6 那么我现在应该做什么?请指导我。
-
你试过在 XCode 中运行“分析”构建吗?因为如果内存永远不会下降,它可能是:1/因为你忘记释放一些数据(如 CoreGraphics 对象)2/设计:如果你持有一些指向 UIImage 的指针。在情况 1/ 中,“分析”应该会迅速警告您。否则,提供更多代码
标签: ios objective-c uiimage