UIImageJPEGRepresentation占用大量内存 [英] UIImageJPEGRepresentation taking huge memory
问题描述
我正在尝试解决此问题,但是在完成我在OS或Google上发现的所有操作后都失败了.问题是,当我使用UIImageJPEGRepresentation
或UIImagePNGRepresentation
将UIImage
转换为NSData
时,它会将内存大小增加到30Mb(信不信由你).
这是我的代码
i'm trying to figure out this problem but failed after doing every thing i found on OS or google. Problem is that when i convert UIImage
to NSData
using UIImageJPEGRepresentation
or UIImagePNGRepresentation
it increases the memory size to 30Mb (believe me or not).
Here is my code
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-内存释放问题
1-二手@autoreleasepool
2-完成pngData = nil;
但仍然面临着内存问题.
I have tried following solution as well
UIImageJPEGRepresentation - memory release issue
1- Used @autoreleasepool
2- done pngData = nil;
but still facing that memory issue.
编辑,我认为我无法传达我的问题.如果UIImageJPEGRepresentation
占用大量内存是可以的,但是,在保存该图像后,内存应回到其较早的位置.希望这会对您有所帮助.
EDIT I think i'm not able to convey my problem. It's ok if UIImageJPEGRepresentation
taking huge memory,but memory should back to it's earlier position after saving that image. Hope this will help you in detail.
推荐答案
使用小于1的scaleValue
.即使0.9,也将以最小的质量损失来大量减少内存占用.
Use a scaleValue
of less than 1. Even 0.9 will massively reduce the memory footprint with minimal quality loss.
这篇关于UIImageJPEGRepresentation占用大量内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!