'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?



Solution 1:[1]

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)

}

Solution 2:[2]

Using Swift 5

   let img  = imageView.image!.pngData()
   let messageStr:String  = "some sharing text"
   let shareItems:Array = [img!, messageStr] as [Any]

   let activityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
    activityViewController.excludedActivityTypes = [UIActivity.ActivityType.print, UIActivity.ActivityType.postToWeibo, UIActivity.ActivityType.copyToPasteboard, UIActivity.ActivityType.addToReadingList, UIActivity.ActivityType.postToVimeo]

    if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone {
        self.present(activityViewController, animated: true, completion: nil)
        } else {
            let popup: UIPopoverController = UIPopoverController(contentViewController: activityViewController)
            popup.present(from: sender.bounds, in: sender, permittedArrowDirections: UIPopoverArrowDirection.any, animated: true)
        }

Solution 3:[3]

Using Swift 5

Note: While sharing a image as Data

func shareApplication() {
    
    let description = "Hi, I have shared my profile"
    
    let url = URL(string: "https://cdn.vox-cdn.com/thumbor/9iH_CxCOzkVQACme6xpCkgzydzo=/0x0:1323x638/920x613/filters:focal(539x248:749x458):format(webp)/cdn.vox-cdn.com/uploads/chorus_image/image/69851120/Screenshot_2021_09_10_7.38.39_AM.0.png")
    
    if let imageData = NSData(contentsOf: url!) {
        
        // MARK: - URL to Data

        let image = UIImage(data: imageData as Data)
        
        let activityVC = UIActivityViewController(activityItems: [description, image ?? UIImage()], applicationActivities: nil)
        self.present(activityVC, animated: true, completion: nil)
    }
}

Solution 4:[4]

It looks like you need to unwrap activityItems with a !

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bruno Bieri
Solution 2 Satyam
Solution 3
Solution 4 Fred Faust