user1949387
user1949387

Reputation: 1275

IOS swift how can I share an image along with text using UIActivityViewController

I have a TableView that has a Text response and an imageView, I want to share both of them. Right now I know how to share the Text response however I do not know how to share the corresponding image . This is the code I have

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "HomePageTVC", for: indexPath) as! HomePageTVC
   // Post Text
   cell.post.text = Posts[indexPath.row]

  // Image View
            let strCellImageURL = self.profile_image_string[indexPath.row]
            let imgURL: NSURL = NSURL(string: strCellImageURL)!
            let request:NSURLRequest =  NSURLRequest(url: imgURL as URL)
            let config = URLSessionConfiguration.default
            let session = URLSession(configuration: config)

            let task = session.dataTask(with: request as URLRequest,    completionHandler: {(data, response, error) in
                DispatchQueue.main.async(execute: { () -> Void in

                    cell.profile_image.image = UIImage(data: data!)


                })
            });
            task.resume()

}

That is my TableView above which displays text and an image in the tableviewCell . I now use this code to share the content of the clicked Table Cell

   @IBAction func Share_Action(_ sender: UIButton) {
// How can I also share a image here ?
        activityViewController = UIActivityViewController(activityItems: [Posts[sender.tag] as NSString], applicationActivities: nil)
        present(activityViewController,animated: true,completion: nil)

    }

As you can see I have the Post text working but now how do I go about sharing the image that is in the TableCell ? For the text I can I get the response by using Posts[sender.tag] for the image the URL I can get by using profile_image_string[indexPath.row] .

Upvotes: 0

Views: 1288

Answers (1)

Yogesh MV
Yogesh MV

Reputation: 1051

For activity items you can give array of values so you can add another object as image

    @IBAction func Share_Action(_ sender: UIButton) {
            let image = profile_image_string[indexPath.row] as UIImage
            activityViewController = UIActivityViewController(activityItems: [Posts[sender.tag] as NSString, image], applicationActivities: nil)
            present(activityViewController,animated: true,completion: nil)

        }

Upvotes: 2

Related Questions