Swift MFMailComposeViewController addAttachmentData JPG设置最大分辨率

时间:2016-08-11 19:31:28

标签: ios swift ios9

首先,我是Swift的新手,如果我做错了,我很抱歉。

我尝试发送包含多个图片作为附件的电子邮件。但我可以添加所有图像,但它们处于全分辨率。

是否可以限制存储在UIImage阵列中的图像的分辨率? (现在带有2张图片的电子邮件将是20MB而不是488KB)

这里有一些代码:

    func configuredMailComposeViewController(image: [UIImage]) -> MFMailComposeViewController {
        let mailComposerVC = MFMailComposeViewController()
        mailComposerVC.mailComposeDelegate = self

        var counter = 1;
        for img in image {
            let imageData: NSData = UIImagePNGRepresentation(img)!
            mailComposerVC.addAttachmentData(imageData, mimeType: "image/jpeg", fileName: "image\(counter).jpg")
            counter += 1
        }

        mailComposerVC.setToRecipients(["email"])
        mailComposerVC.setSubject("subject")
        mailComposerVC.setMessageBody("message", isHTML: true)

        return mailComposerVC
    }

图像将在此处创建:

@IBAction func takePhoto(sender: UIButton) {
        imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .Camera

        presentViewController(imagePicker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    imagePicker.dismissViewControllerAnimated(true, completion: nil)

    var image_taken: UIImage!
    image_taken = info[UIImagePickerControllerOriginalImage] as? UIImage

    var image: UIImage!
    image = image_taken

    imageArray.append(image)

}


var imagePicker: UIImagePickerController!

现在调用createEmail方法:

//send data to email
@IBAction func sendDataToServer(sender: UIBarButtonItem) {
    composeMail(imageArray)
}

1 个答案:

答案 0 :(得分:2)

您可以使用压缩来缩小其大小:

let imageData: NSData = (UIImageJPEGRepresentation(imgView.image!, 0.5) as? NSData)!
mailComposer.addAttachmentData(imageData as Data, mimeType: "image/jpeg", fileName: "image.jpg")