- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试下载一些图像(从 base64 字符串转换为 UIImage)并将其保存到设备,但不断收到内存警告。
ontracFullScreenImageViewController *etrackDiagrams = ((ontracFullScreenImageViewController*)[viewControllerDictionary objectForKey:@"E-track Diagrams"]);
NSMutableSet *etrackSet = [[NSMutableSet alloc] init];
for (UIImage *image in etrackDiagrams.imageArray) {
NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];
//convert the image to NSData and store it in the documents directory
NSData *pngData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString * timeInMS = [NSString stringWithFormat:@"%lld", [@(floor([[NSDate date] timeIntervalSince1970] * 1000)) longLongValue]];
NSString *filePath = [documentsPath stringByAppendingPathComponent:[ NSString stringWithFormat:@"%@_%@_etrack_diagram_%i_%i_image.png", delegate.userName, timeInMS, self.dataObject.dataPack.pack_id, [etrackDiagrams.imageViewArray indexOfObject:image]]]; //Add the file name
[pngData writeToFile:filePath atomically:YES];
NSLog(@"filepath %@", filePath);
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer isEqualToString:@"5.0.1"]) {
[[NSURL URLWithString:filePath] setResourceValue: [NSNumber numberWithBool: YES] forKey: NSURLIsExcludedFromBackupKey error: &error];
}
//Add the file Path to ImageLinks
[etrackDiagrams.imageLinks addObject:filePath];
//save the image location in Core Data
EtrackDiagram *etrackDiagram = [NSEntityDescription
insertNewObjectForEntityForName:@"EtrackDiagram"
inManagedObjectContext:context];
etrackDiagram.locationString = filePath;
etrackDiagram.dataObject = dataObject;
[etrackSet addObject:etrackDiagram];
[dataObject addEtrackDiagramsObject:etrackDiagram];
[localPool drain];
}
[dataObject addEtrackDiagrams: etrackSet];
内存警告发生在 NSData *pngData = UIImagePNGRepresentation(image);
处,因为图像非常大。
不幸的是,我无法控制图像的大小,但需要一种方法将它们保存到设备上,以便稍后在图库中使用它们。
我尝试用 @autoreleasepool
包装代码,但没有什么区别。
最佳答案
真正的问题是这一行:
for (UIImage *image in etrackDiagrams.imageArray) {
这意味着您在内存中保存了一组图像。那总是很危险的。您需要将所有这些图像保存在磁盘上并一次将它们加载到内存中(并且以非缓存方式)。您的目标应该是一次最多在内存中保存一张图像。
关于ios - Objective-c UIImagePNGRepresentation 导致内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39977524/
我有一些透明图像,正在从文件系统加载到 UIImageView View 中。出于我的目的,我需要将 UIImageView 中的图像与文件系统上的文件进行比较。所以我做了如下的事情: NSStrin
我正在开发一个使用相机拍照的 iPhone 应用程序,然后将它们保存到应用程序文档目录中。我使用以下代码将 UIImage 转换为 NSData, NSData *imageData = [NSDat
我发现这个模块很麻烦。我从 Photolibrary 导入了 100 多张图片,将它们保存在不同名称的文档目录中。不出所料,我在不寻常的地方遇到了内存问题。似乎 UIImagePNGRepresena
使用此代码,我从共享扩展中提取一张图像,并将其写入我在应用程序组中创建的目录。 let content = self.extensionContext!.inputItems[0] as! NSExt
我意识到之前有人问过这个问题,但我没有找到任何真正解决它的答案,所以......再问一次。 我正在使用 ARC。我的应用程序定期从 AVCaptureSession 中拍摄照片并将它们保存到核心数据中
我正在开发一个使用网络协议(protocol) UPnP 连接多个设备的应用程序。 除其他外,我可以交换图像,它们是存储在我的项目文件夹中的文件夹中的文件。 为了发送图像,我得到了预定义的方法。 if
如果我们使用 UIImagePNGRepresentation 转换 JPEG 文件,那么新文件的大小会更大。如果我们使用 UIImagePNGRepresentation 转换 PNG 文件,那么新
我正在尝试将图像从 UIImagePicker 复制到文档目录。我正在使用 @"UIImagePickerControllerOriginalImage" 键从 UIImagePickerDelega
我正在尝试制作缩略图并保存到文档目录。但问题是当我试图将缩略图图像转换为 NSData 时。它返回零。 这是我的代码, UIImage *thumbNailimage=[image thumbna
每次我使用相机拍照,然后保存时,图像总是横向的。这意味着我的 Xib 中的 UIImageView 是错误的。它的肖像 - 这就是我想要和期望的。我可以通过将图像旋转 90 度来纠正此问题,但即使如此
我在做一些我认为不应该那么困难的事情上遇到了困难,所以我想我一定是从错误的角度看待问题。为了了解 AVCaptureStillImageOutput 和相机的工作原理,我制作了一个小应用程序。 这个应
我在我的代码中使用了下面一行 NSData *dataForImage=UIImagePNGRepresentation(image); 我的应用程序因内存不足而崩溃。当我使用 VM 跟踪器/分配
我有一个应该代表复选框的按钮。单击按钮时,imageView.image-Property 将设置为另一个 UIImage。因此,我有两个变量用于已检查和未检查图像。 UIImage *checked
图片来自UIImagePickerViewController: func imagePickerController(picker: UIImagePickerController, didFini
我的 iphone 照片应用程序中有图像文件。 我已经通过空投将它带到我的 macbook 上,并检查了图像的大小 = 1.9 MB 我将相同的图像保存到 iphone 的文件应用程序并检查了那里的图
我将新收到的 PDF 文档的缩略图呈现到文档目录中。 我正在使用以下代码: CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundl
使用 Swift 我尝试从 URL 下载 JPG 然后将该图像保存到文件,但是当我尝试将它保存到不同的目录时它惯于。它将下载到应用程序的 Documents 文件夹,但当我尝试将路径设置为另一个子文件
我正在尝试下载一些图像(从 base64 字符串转换为 UIImage)并将其保存到设备,但不断收到内存警告。 ontracFullScreenImageViewController *etrackD
这就是我从 UIImage 创建 Data 的方式: let data = UIImagePNGRepresentation(image) 然后我需要将它转换成String; if let d
代码 static func encodeImage(image: UIImage) -> String{ var imageData = UIImagePNGRepresentation(i
我是一名优秀的程序员,十分优秀!