Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing image using UIActivityViewController

Sharing a image from UIImageView using UIActivityViewController?

I am trying to share a image using UIActivityViewController, but I get error:

func shareIMG(){
    let activityItems = [self.imageView.image]
    let avc = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)

    self.presentViewController(avc, animated: true, completion: nil)
}

Error message:

Cannot find an initializer for type 'UIActivityViewController' that accepts an argument list of type '(activityItems: [UIImage?], applicationActivities: nil)'

Any ideas?

like image 971
Eri-Sklii Avatar asked Aug 12 '15 02:08

Eri-Sklii


2 Answers

let activityItem: [AnyObject] = [self.imageView.image as! AnyObject]

let avc = UIActivityViewController(activityItems: activityItem as [AnyObject], applicationActivities: nil)

self.presentViewController(avc, animated: true, completion: nil)

Note : The above code tested in Xcode 6.3 simulator

like image 108
karthikPrabhu Alagu Avatar answered Oct 18 '22 02:10

karthikPrabhu Alagu


Just create a button and it's action. And install the button's action like this.

Note: You have to log in your facebook or twitter by going to setting. Then do like this.

@IBAction func onShareTouched(sender: AnyObject) {

    print("share")

    let myShare = "My beautiful photo! <3 <3"
    let image: UIImage = UIImage(named: "yourImageNameHere")

    let shareVC: UIActivityViewController = UIActivityViewController(activityItems: [(image), myShare], applicationActivities: nil)
    self.presentViewController(shareVC, animated: true, completion: nil)

}
like image 38
Khuong Avatar answered Oct 18 '22 03:10

Khuong