2016-07-14 7 views
2

あなたのアプリでメールを送信する際に、たくさんのチュートリアルを試しましたが、見たことのある画像はありません。 .WriteToFileからイメージを回復しています。このイメージはUIImageViewに設定されています。自分の写真と一緒にメールを送信するにはどうすればよいですか?Swiftに添付された画像であなたのアプリからメールを送信する

ニール

答えて

8

あなたはNSDataの中で、あなたのイメージ をコードする、あなたのメールにattachmentDataを追加する必要があります。これはあなたのイメージを でメールで送信する方法を示す例です。私はUIViewControllerがあり、そこにはsendMailという関数を置くことができます。 VCを解任するために

import MessageUI 
class MyViewController: UIViewController, MFMailComposeViewControllerDelegate 
{ 
    // .... your stuff 

    func sendMail(imageView: UIImageView) { 
    if MFMailComposeViewController.canSendMail() { 
     let mail = MFMailComposeViewController() 
     mail.mailComposeDelegate = self; 
     mail.setCcRecipients(["[email protected]"]) 
     mail.setSubject("Your messagge") 
     mail.setMessageBody("Message body", isHTML: false) 
     let imageData: NSData = UIImagePNGRepresentation(imageView.image)! 
     mail.addAttachmentData(imageData, mimeType: "image/png", fileName: "imageName") 
     self.presentViewController(mail, animated: true, completion: nil) 
    } 
    } 

} 

は、あなたのViewControllerに以下の方法を含む:

func mailComposeController(controller: MFMailComposeViewController, 
    didFinishWithResult result: MFMailComposeResult, error: NSError?) { 
     controller.dismissViewControllerAnimated(true, completion: nil) 
} 

をこのメソッドのドキュメントはMFMailComposeViewControllerDelegateで報告されます。

+0

キャンセルを押したときにメールフォームを却下するにはどうすればよいですか?スクリーンショット:https://postimg.org/image/6pz5yv5lj/ –

+0

私は答えを更新しました。私の編集を参照してください。 –

+0

ありがとうAdda_25これは多くの助けになりました。 –

関連する問題