IOS swift如何使用UIActivityViewController共享图像和文本
我有一个TableView,它有一个文本响应和一个imageView,我想共享这两个。现在我知道如何共享文本响应,但是我不知道如何共享相应的图像。这是我的密码IOS swift如何使用UIActivityViewController共享图像和文本,ios,swift,uitableview,swift3,uiactivityviewcontroller,Ios,Swift,Uitableview,Swift3,Uiactivityviewcontroller,我有一个TableView,它有一个文本响应和一个imageView,我想共享这两个。现在我知道如何共享文本响应,但是我不知道如何共享相应的图像。这是我的密码 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "
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中的图像?对于文本,我可以使用Posts[sender.tag]获取响应。对于图像,我可以使用profile\u image\u string[indexPath.row]获取URL。对于活动项,您可以提供值数组,以便可以添加另一个对象作为图像
@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)
}
对于活动项,您可以提供值数组,以便可以添加另一个对象作为图像
@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)
}
哦,好的,我知道如何正确地获取indexPath了,我已经完成了这项工作,让indexPath:NSIndexPath您可以从发送者标记获取indexPath,比如让indexPath=NSIndexPath(行:0,节:0)让cell=tableView.cellForRow(at:indexPath as indexPath)as!HomePageTVC let image=cell.profile\u image.image注意:如果未下载图像,则无法从imageView获取图像。因此,您也可以在这里执行下载逻辑。哦,好的,我知道如何正确地获取其中的indexPath。到目前为止,我已经完成了这项工作,让indexPath:NSIndexPath您可以从发送者标记获取indexPath,比如让indexPath=NSIndexPath(行:0,节:0)让cell=tableView.cellForRow(at:indexPath作为indexPath)as!HomePageTVC let image=cell.profile\u image.image注意:如果未下载图像,则无法从imageView获取图像。因此,您也可以在这里执行下载逻辑。