Ios 使用大量内存的UIImageJPEG表示法(Swift 3.0)

Ios 使用大量内存的UIImageJPEG表示法(Swift 3.0),ios,swift,xcode,swift3,Ios,Swift,Xcode,Swift3,我正试图用一个“for循环”从20到30个UIImage中压缩和获取NSdata,如下所示: for theImage in selectedUIImages { let data = UIImageJPEGRepresentation(image,0.5) // doing something with the data } 在iPhone 7上尝试,除了我的应用程序外,没有任何问题,在循环过程中使用高达700MB的内存,但在旧版iPhone上,我收到了以下信息: *Message fr

我正试图用一个“for循环”从20到30个UIImage中压缩和获取NSdata,如下所示:

for theImage in selectedUIImages {

let data = UIImageJPEGRepresentation(image,0.5)
// doing something with the data

}
在iPhone 7上尝试,除了我的应用程序外,没有任何问题,在循环过程中使用高达700MB的内存,但在旧版iPhone上,我收到了以下信息:

*Message from debugger: Terminated due to memory issue.*
主要目的是从UIImage中获取NSData,这样我就可以将图像放在dir中进行上传。让我解释一下:

Amazon S3传输实用程序需要图像的路径/url,因此我需要为UIImage创建路径/url,我知道的唯一方法是通过以下方式获得它:

data.write(to: URL(fileURLWithPath: localPath), options: .atomic)

因为你的应用程序内存不足。
压缩后可以保存到文档目录,然后逐个上传到服务器。这样就不会造成内存问题。

您可以通过减小比率参数来减小图像大小。可以使用0.3而不是0.5

for theImage in selectedUIImages {

let data = UIImageJPEGRepresentation(image,0.3)
// doing something with the data

}

尝试使用自动释放池:

autoreleasepool {
    for theImage in selectedUIImages {

        let data = UIImageJPEGRepresentation(image,0.5)
        // doing something with the data

     }
}

并在后台线程中移动它。

您在设备中试用过吗?可能是的副本。普里亚尔:什么意思?马丁:这是给swift 3的。0@NSNoob谢谢你问配偶:-)让我解释一下。Amazon S3传输实用程序需要图像的路径/url,因此我需要为UIimage创建路径/url,我知道的唯一方法是通过以下方式获取它:data.write(to:url(fileURLWithPath:localPath),options:.atomic)将自动删除池放在循环中不是更好吗?与以前相同的错误:(来自调试器的消息:由于内存问题而终止。@fayyaz您将继续收到该消息,因为您无法控制selectedUIImages数组中的图像数量。它可能适用于多个图像,并开始在多个图像上崩溃。因此最好先压缩然后上载。因此基本上是1.a for循环t帽子先压缩,然后再上传?