How to add a image in email body using MFMailComposeViewController

前端 未结 4 1621
故里飘歌
故里飘歌 2020-12-01 05:31

I am trying to find out the best way to add an image inside the body of the email and not as attachment in ios.

1) Apple has provided a function \"addAttach

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 06:03

    I just went through this recently for Swift.

    Function to add photo to email in Swift:

    func postEmail() {
        var mail:MFMailComposeViewController = MFMailComposeViewController()
        mail.mailComposeDelegate = self
        mail.setSubject("your subject here")
    
        var image = // your image here
        var imageString = returnEmailStringBase64EncodedImage(image)
        var emailBody = ""
    
        mail.setMessageBody(emailBody, isHTML:true)
    
        self.presentViewController(mail, animated: true, completion:nil)
    }
    

    Function to return the formatted image:

    func returnEmailStringBase64EncodedImage(image:UIImage) -> String {
        let imgData:NSData = UIImagePNGRepresentation(image)!;
        let dataString = imgData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
        return dataString
    }
    

提交回复
热议问题