MFMailComposeViewController 需要很长时间才能发送电子邮件

MFMailComposeViewController taking ages to send email

我正在构建一个应用程序,我需要在其中附加并在电子邮件中发送多张照片。为此,我将照片保存到相机 VC 中的磁盘,并在电子邮件 VC 的 viewDidLoad() 中访问它们。我将它们作为 .pngData() 存储在一个数组中,并使用以下代码将它们附加到 MFMailComposeViewController 中:

func configuredMailComposeViewController() -> MFMailComposeViewController {

    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self

    mailComposerVC.setToRecipients([UserDefaults.standard.string(forKey: "Email")!])
    mailComposerVC.setSubject("Заявление")
    mailComposerVC.setMessageBody("\(ProtocolText.text!)", isHTML: false)

    // unpacking images from array and attaching them to Email

    var dataName = 0
    for data in imageData {
        dataName += 1
        mailComposerVC.addAttachmentData(data, mimeType: "image/png", fileName: "\(dataName).png")
    }

    return mailComposerVC

}

这些是我的 "save" 和 "get" 方法:

func loadImage(fileName: String) -> UIImage? {

    let documentDirectory = FileManager.SearchPathDirectory.documentDirectory

    let userDomainMask = FileManager.SearchPathDomainMask.userDomainMask
    let paths = NSSearchPathForDirectoriesInDomains(documentDirectory, userDomainMask, true)

    if let dirPath = paths.first {
        let imageUrl = URL(fileURLWithPath: dirPath).appendingPathComponent(fileName)
        let image = UIImage(contentsOfFile: imageUrl.path)
        return image

    }

    return nil
}

    // Used in CameraViewController
func saveImage(imageName: String, image: UIImage) {


    guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }

    let fileName = imageName
    let fileURL = documentsDirectory.appendingPathComponent(fileName)
    guard let data = image.jpegData(compressionQuality: 1) else { return }

    //Checks if file exists, removes it if so.
    if FileManager.default.fileExists(atPath: fileURL.path) {
        do {
            try FileManager.default.removeItem(atPath: fileURL.path)
            print("Removed old image")
        } catch let removeError {
            print("couldn't remove file at path", removeError)
        }

    }

    do {
        try data.write(to: fileURL)
    } catch let error {
        print("error saving file with error", error)
    }

}

由于某种原因,图像已附加,但电子邮件至少需要 15 秒才能发送。 "send" 按钮卡住了。我不知道为什么。请帮忙

是图片权重的问题。一张图片曾经重 6848.7939453125 KB。由于我有多个图像,因此需要花费大量时间来处理它们。使用 image.jpegData(compressionQuality: CGFloat)!) 方法压缩它们解决了所有问题。感谢 Mark Thormann 提出修复建议!