gpt4 book ai didi

ios - Swift MFMailComposeViewController addAttachmentData JPG 设置最大分辨率

转载 作者:搜寻专家 更新时间:2023-11-01 07:21:10 25 4
gpt4 key购买 nike

首先,我是 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)
}

最佳答案

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

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

关于ios - Swift MFMailComposeViewController addAttachmentData JPG 设置最大分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38904684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com