ios - How to attach image from ImageView to E-mail within an App -
got following. made simple ios / swift app needs send image specific e-mail. got working:
- take picture
- grap picture existing photo's
- image shown in image view
- send button thats leads me mail configured: recipient, subject , messagebody.
what need working how can add selected image image view added e-mail when press send.
following code 1 use:
for taking , selecting images:
@iboutlet var imageview: uiimageview! @iboutlet weak var piclabel: uilabel! override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } @ibaction func fotoknop(sender: anyobject) { } @ibaction func chooseimagefromphotolibrary() { let picker = uiimagepickercontroller() picker.delegate = self picker.sourcetype = .photolibrary presentviewcontroller(picker, animated: true, completion: nil) } @ibaction func choosefromcamera() { let picker = uiimagepickercontroller() picker.delegate = self picker.sourcetype = .camera presentviewcontroller(picker, animated: true, completion: nil) } func imagepickercontroller(picker: uiimagepickercontroller, didfinishpickingimage image: uiimage!, editinginfo: [nsobject : anyobject]!) { imageview.image = image self.dismissviewcontrolleranimated(true, completion: nil) }
for e-mail
@ibaction func sendemailbuttontapped(sender: anyobject) { let mailcomposeviewcontroller = configuredmailcomposeviewcontroller() if mfmailcomposeviewcontroller.cansendmail() { self.presentviewcontroller(mailcomposeviewcontroller, animated: true, completion: nil) } else { } } func configuredmailcomposeviewcontroller() -> mfmailcomposeviewcontroller { let mailcomposervc = mfmailcomposeviewcontroller() mailcomposervc.mailcomposedelegate = self // extremely important set --mailcomposedelegate-- property, not --delegate-- property mailcomposervc.settorecipients(["jvanhattem@it-serve.nl"]) mailcomposervc.setsubject("mail vanuit picmail") mailcomposervc.setmessagebody("onderstaand de doorgestuurde informatie", ishtml: false) return mailcomposervc } func showsendmailerroralert() { let sendmailerroralert = uialertview(title: "could not send email", message: "your device not send e-mail. please check e-mail configuration , try again.", delegate: self, cancelbuttontitle: "ok") sendmailerroralert.show() } // mark: mfmailcomposeviewcontrollerdelegate method func mailcomposecontroller(controller: mfmailcomposeviewcontroller!, didfinishwithresult result: mfmailcomposeresult, error: nserror!) { controller.dismissviewcontrolleranimated(true, completion: nil) } }
any thoughts great!
func configuredmailcomposeviewcontroller() -> mfmailcomposeviewcontroller { let mailcomposervc = mfmailcomposeviewcontroller() mailcomposervc.mailcomposedelegate = self // extremely important set --mailcomposedelegate-- property, not --delegate-- property mailcomposervc.settorecipients(["jvanhattem@it-serve.nl"]) mailcomposervc.setsubject("mail vanuit picmail") mailcomposervc.setmessagebody("onderstaand de doorgestuurde informatie", ishtml: false) //add image attachment if let image = imageview.image { let data = uiimagejpegrepresentation(image, 1.0) mailcomposervc.addattachmentdata(data!, mimetype: "image/jpg", filename: "image") } return mailcomposervc }
Comments
Post a Comment