user1591668
user1591668

Reputation: 2883

IOS Swift UIActivityViewController can share Text but not images in tableView

I have a UITableView that has text and images . I can share text however with images I get a crash because of nil values . The images are downloaded already but I can't seem to have them working this is my code

@IBAction func shareMyAction(_ sender: UIButton) {

    let message = streamsModel.posting[sender.tag]

    if streamsModel.stream_image_string[sender.tag].characters.count > 6 {
       // Has image
        let cell = sender.superview?.superview as? SocialCellView
        if cell == nil {
            print("cell is nil")
        }

      let myImage = cell?.stream_image.image

        if myImage == nil {
            print("myImage is NIL")
        }

        activityViewController = UIActivityViewController(activityItems: [streamsModel.Posts[sender.tag].appending(message) as NSString,myImage!], applicationActivities: nil)

    } else {
        activityViewController = UIActivityViewController(activityItems: [streamsModel.Posts[sender.tag].appending(message) as NSString], applicationActivities: nil)

    }


}

On the code above I also get nil on Cell and myImage . Again the images display on the TableView but can't seem to get them to work with UIActivityViewController .

Upvotes: 1

Views: 449

Answers (1)

Reinier Melian
Reinier Melian

Reputation: 20804

Based on your comment that your button is on your cell directly you issue is related to this line let cell = sender.superview?.superview as? SocialCellView you are adding an additional superview so you need to modify your code to this one as I said in my previous comments

 @IBAction func shareMyAction(_ sender: UIButton) {

        let message = streamsModel.posting[sender.tag]

        if streamsModel.stream_image_string[sender.tag].characters.count > 6 {
           // Has image
            let cell = sender.superview as? SocialCellView
            if cell == nil {
                print("cell is nil")
            }

          let myImage = cell?.stream_image.image

            if myImage == nil {
                print("myImage is NIL")
            }

            activityViewController = UIActivityViewController(activityItems: [streamsModel.Posts[sender.tag].appending(message) as NSString,myImage!], applicationActivities: nil)

        } else {
            activityViewController = UIActivityViewController(activityItems: [streamsModel.Posts[sender.tag].appending(message) as NSString], applicationActivities: nil)

        }


    }

Hope this helps

Upvotes: 1

Related Questions