2016-03-21 14 views
0

キャプチャした画像をSendGridを使って電子メールの添付ファイルとして送信しようとしています。私は電子メールのsendmail関数とattachImageメソッドを定義しました。sendgridで画像をキャプチャしました

public extension SendGrid { 
    public class Email { 
    ...... 
     public var images: [UIImage]! 

     public func attachImage(image: UIImage) { 
      if self.images == nil { 
       self.images = [] 
      } 
      self.images.append(image) 
     } 
    } 
} 

はその後

var sg = SendGrid(username: "*****", password: "*****") 
var email = SendGrid.Email() 

func sendMail(){ 
    screenShot() 

    do { 
     try 
     email.addTo("[email protected]", name: "Sehwan") 
     email.setFrom("[email protected]", name: "test") 
     email.setSubject("Hello Images") 
     email.setHtmlBody("<p>Up Thai, Wolfgang, Benihana, Wa.</p><br><br><p>Nice it works</p>") 
     email.attachImage(shareImage) 
    } catch { 
     print(error) 
    } 

    if(shareImage != nil){ 
     do { 
      try 
      sg.send(email, completionHandler: { (response, data, error) -> Void in 
       if let json = NSString(data: data!, encoding: NSUTF8StringEncoding) { 
        print(json) 
       } 
      }) 
     } catch { 
      print(error) 
     } 
    } 
} 

func screenShot(){ 
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(backgroundImage.frame.size.width, backgroundImage.frame.size.width), false, 0) 
    self.view?.drawViewHierarchyInRect(CGRectMake(0, -40, view.bounds.size.width, view.bounds.size.height), afterScreenUpdates: true) 
    shareImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
} 

この関数を呼び出すすべてがうまく機能 - 私はそれを共有したり、携帯電話に保存することができますので、スクリーンショット機能は、画面の一部をキャプチャします。また、電子メールは画像ファイルなしで提供されることを除いて、一部の電子メールを扱う。

sendGridを使用して、キャプチャされた画像を添付ファイルとして電子メールで送信する方法はわかりません。どんなアドバイスも大変ありがとうございます。

答えて

0

API referenceは、これを行う方法を正確に説明しています。

関連のあるセクション:

ファイルは7メガバイトのファイルが添付されるよりも小さくなければなりません。

ファイルの内容は、マルチパートHTTP POSTの一部でなければなりません。

例:ファイル[file1.jpg] = file1.jpg &ファイル[file2.pdf] = file2.pdf

+0

その例はすでに名前とパスを持つファイルのためのものです。キャプチャした画像を印刷すると、「 size {375、375} orientation 0 scale 2.000000」となります。アプリでキャプチャした画像ファイルへのパスを与える方法はありますか? –

+0

btw、どうもありがとうございました!コードからIDとパスワードを削除するのを忘れてしまった。再び、ありがとう –

関連する問題