Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios MFMailComposeViewController发送电子邮件需要很长时间_Ios_Swift_Mfmailcomposeviewcontroller_Messageui - Fatal编程技术网

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

Ios MFMailComposeViewController发送电子邮件需要很长时间,ios,swift,mfmailcomposeviewcontroller,messageui,Ios,Swift,Mfmailcomposeviewcontroller,Messageui,我正在构建一个应用程序,在其中我需要在电子邮件中附加和发送多张照片。为此,我将照片保存到camera VC中的磁盘,并在电子邮件VC的viewDidLoad()中访问它们。我将它们存储为数组中的.pngData(),并使用以下代码将它们附加到MFMailComposeViewController中: func configuredMailComposeViewController() -> MFMailComposeViewController { let mailCompose

我正在构建一个应用程序,在其中我需要在电子邮件中附加和发送多张照片。为此,我将照片保存到camera 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

}
以下是我的“保存”和“获取”方法:

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秒。“发送”按钮卡住了。我不知道为什么。请帮助。

这是图像的重量问题。一幅图像的重量为6848.7939453125 KB。因为我有多张图片,所以处理它们花了很多时间。使用
image.jpegData压缩它们(压缩质量:CGFloat)!)方法解决了所有问题。谢谢你建议修复

这些图像有多大?上面的代码还让您将文件保存为JPEG格式,并将其附加为PNG格式。使用pngData而不是JPEG数据是否更好?谢谢!问题已解决。我认为由于文件大小的原因,速度非常慢。压缩质量为0.75时,滞后显著减小。