Wednesday, November 11, 2015

Pick a photo from the device album and email it as attachment

This post is written with Xcode 7.1/Swift 2.1.

Edit ViewController.swift as below:

import UIKit
import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate,UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    var myUIImage : UIImage!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let button = UIButton(frame: CGRectMake(70, 100, 200, 20))
        button.setTitle("Pick a Photo", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "photoAlbum:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(button)
    }
    
    func photoAlbum(sender: UIButton) {
        if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum)) {
            let pickerController = UIImagePickerController()
            pickerController.delegate = self
            self.presentViewController(pickerController, animated: true, completion: nil)
        }
    }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        myUIImage = image
        picker.dismissViewControllerAnimated(true, completion: {
            dispatch_async(dispatch_get_main_queue(), {
                self.emailSetup()
            })
        })

    }
    
    func emailSetup() {
        
        let myController = MFMailComposeViewController()
        myController.mailComposeDelegate = self
        myController.setSubject("My Photo")
        myController.setMessageBody("Picked from my photo album.", isHTML: false)
        myController.setToRecipients(["mymail@mymail.com"])
        
        let imageData = UIImagePNGRepresentation(myUIImage)
        myController.addAttachmentData(imageData!, mimeType: "image/jpeg", fileName: "image")
        
        self.presentViewController(myController, animated: true, completion: nil)
    }
    
    func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
        if result.rawValue == MFMailComposeResultSent.rawValue {
            self.dismissViewControllerAnimated(false, completion: {
                
                dispatch_async(dispatch_get_main_queue(), {
                
                    let alertController = UIAlertController(title: "", message: "Mail Sent!", preferredStyle: UIAlertControllerStyle.Alert)
                    alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
            
                    self.presentViewController(alertController, animated:true, completion:nil)
                })
            })
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

Related Posts:

No comments:

Post a Comment