使用MFMailComposeViewController附加图像从PhotoGallery到电子邮件
我希望用户从照片库中选择其所需的图像,然后将这些图像包含在他们将要发送的电子邮件中。
我已经创建了一个按钮,该按钮使用户可以打开图库,然后在选择图像后将其显示在UIImageView中。但是,我认为我撰写的代码是不正确的。
UIimageViews名称是ImageView
按钮以打开画廊
@IBAction func openPhotoLibraryButton(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary;
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
}
电子邮件代码
@IBAction func sendEmail(_ sender: Any) {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
let emailFullName: String = fullName.text!
let emailCell: String = cellNumber.text!
let image1: UIImage = imageView.image!
mail.mailComposeDelegate = self
//Reciepient
mail.setToRecipients(["[email protected]"])
// Email body
mail.setMessageBody("FullName: \(emailFullName) \n Cellphone No: \(emailCell)" , isHTML: false)
// Subject
mail.setSubject("Application for RTO: ")
// Email attachment
mail.addAttachmentData(Data, mimeType: "image/jpg", fileName: "\(image1)")
self.present(mail, animated: true, completion: nil)
}
else {
print("Email cannot be sent")
}
}
I would like for user to choose their desired image from photo gallery and then that image is included in the email that they're are about to send.
I've already created a button that enables user to open gallery and then after choosing image it is displayed in UIImageView. However, my code that I have written in addAttachment I think is incorrect.
The UIImageViews name is imageView
button to open gallery
@IBAction func openPhotoLibraryButton(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary;
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
}
Email code
@IBAction func sendEmail(_ sender: Any) {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
let emailFullName: String = fullName.text!
let emailCell: String = cellNumber.text!
let image1: UIImage = imageView.image!
mail.mailComposeDelegate = self
//Reciepient
mail.setToRecipients(["[email protected]"])
// Email body
mail.setMessageBody("FullName: \(emailFullName) \n Cellphone No: \(emailCell)" , isHTML: false)
// Subject
mail.setSubject("Application for RTO: ")
// Email attachment
mail.addAttachmentData(Data, mimeType: "image/jpg", fileName: "\(image1)")
self.present(mail, animated: true, completion: nil)
}
else {
print("Email cannot be sent")
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论