我有一个TableView,它有一个Text响应和一个imageView,我想分享它们。现在我知道如何分享文本响应,但我不知道如何共享相应的图像。这是我的代码
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()
}
这是我上面的TableView,它在tableviewCell中显示文本和图像。我现在使用此代码来共享所单击的表格单元格的内容
@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)
}
正如您所看到的,我发布了Post文本但现在如何分享TableCell中的图像?对于文本,我可以通过使用 profile_image_string [indexPath.row] ,使用帖子[sender.tag] 获取我可以获得的URL的图像。
答案 0 :(得分:2)
对于活动项目,您可以提供值数组,以便可以将另一个对象添加为图像
@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)
}