ios – UIImageJPEGRepresentation占用大量内存

ios – UIImageJPEGRepresentation占用大量内存,第1张

概述我试图找出这个问题,但在做了我在OS或谷歌上发现的所有事情后失败了.问题是,当我使用UI ImageJPEGRepresentation或UIImagePNGRepresentation将UIImage转换为NSData时,它会将内存大小增加到30Mb(相信我或不相信). 这是我的代码 myImage= image;LoginSinglton*loginObj = [LoginSinglton 我试图找出这个问题,但在做了我在OS或谷歌上发现的所有事情后失败了.问题是,当我使用UI ImageJPEGRepresentation或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 directoryself.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占用大量内存,那就没关系,但是在保存该图像后,内存应该回到它的早期位置.希望这会对你有所帮助.

解决方法 使用小于1的scaleValue.即使0.9也会大大减少内存占用,同时将质量损失降至最低. 总结

以上是内存溢出为你收集整理的ios – UIImageJPEGRepresentation占用大量内存全部内容,希望文章能够帮你解决ios – UIImageJPEGRepresentation占用大量内存所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1076671.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存