从数据数组(coreData图像)创建Zip文件,并通过电子邮件通过.zip文件共享

A. *_*ini 8 core-data uiimage nsdata swift

我的CoreData中有(n)个数据(UIImage JPEG)。

let imageData: [Data]... 
Run Code Online (Sandbox Code Playgroud)

我已经有了这两个框架/ PodZipZIPFoundation

我对此有一些疑问:

  1. 我需要为每个imageData创建一个临时URL吗?
  2. 如果是,我必须tempURL.appendingPathExtension("jpg")在呼叫之前或之后添加到每个临时URL data.write(to: tempURL)

在那之后,我有了一个URL数组,所以我只需要创建一个Zip文件并共享它。但这不起作用,我的Mac上出现一个.zip-.cpgz循环。

 private func createURLsFrom(imageData: [ImageData]?) {
    var urlArray = [URL]()
    imageData?.forEach { imData in
        if let data = imData.imageData,
        let tempURL = NSURL.fileURL(withPathComponents: [NSTemporaryDirectory(), NSUUID().uuidString])?.appendingPathExtension("jpg") {

            do {
                try data.write(to: tempURL)
                urlArray.append(tempURL)

            } catch {...}
        }
    }
    self.createZipFile(urlArray: urlArray)
}



private func createZipFile(urlArray: [URL]) {
    if let zipURL = try? Zip.quickZipFiles(urlArray, fileName: "ZIP_Test1") {
        self.sendEmailWith(dataURL: zipURL)
    } else {...}
}


private func sendEmailWith(dataURL: URL) {
    if MFMailComposeViewController.canSendMail() {

        let mailComposer = MFMailComposeViewController()
        mailComposer.mailComposeDelegate = self

        mailComposer.setSubject("setSubject")
        mailComposer.setMessageBody("setMessageBody", isHTML: false)
        mailComposer.addAttachmentData(dataURL.dataRepresentation, mimeType: "application/zip", fileName: ("ZIP_Test1.zip"))

        self.present(mailComposer, animated: true, completion: nil)
    }
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么 :(

Jon*_*ose -1

移动

self.createZipFile(urlArray: urlArray)
Run Code Online (Sandbox Code Playgroud)

forEach循环之后。