Marius Hegg
Marius Hegg

Reputation: 99

Share an image using UIActivityViewController

I want to share an image using UIActivityViewController. I am able to display the text I want to share, but I can't really figure out how to add an image. Hope somebody could help me. Thanks!

My code is displayed under "actionButton"

import UIKit

class DetailViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var myDetailedImageView: UIImageView!

    var myDetailedImageName: String?
    var nameString: String?

    override func prefersStatusBarHidden() -> Bool {
        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //For setting max and min zoom
        scrollView.maximumZoomScale = 0.8
        scrollView.minimumZoomScale = 0.15
        scrollView.delegate = self
        //reset zoomzcale for new image
        self.scrollView.zoomScale = 0.15
        self.myDetailedImageView.image = UIImage(named: myDetailedImageName!)
    }

    @IBAction func actionButton(sender: AnyObject) {
        let firstActivityItem = NSString(string: myDetailedImageName!)
        let activityViewController : UIActivityViewController = UIActivityViewController(activityItems: [firstActivityItem], applicationActivities: nil)
        self.presentViewController(activityViewController, animated: true, completion: nil)
    }

    //Showing what to zoom/scroll
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        //returning image to reload it self
        return myDetailedImageView
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Upvotes: 1

Views: 1121

Answers (1)

Azat
Azat

Reputation: 6795

Just like with a text:

let activityViewController = UIActivityViewController(activityItems: [firstActivityItem, self.myDetailedImageView.image!], applicationActivities: nil)

Upvotes: 1

Related Questions