IOS Swift UIActivityViewController 可以在 tableView 中共享文本但不能共享图像
IOS Swift UIActivityViewController can share Text but not images in tableView
我有一个包含文本和图像的 UITableView。我可以与图像共享文本,但由于 nil 值,我会崩溃。图片已经下载,但我似乎无法让它们工作这是我的代码
@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)
}
}
在上面的代码中,我在 Cell 和 myImage 上也得到了 nil。图像再次显示在 TableView 上,但似乎无法让它们与 UIActivityViewController 一起工作。
根据你的评论,你的按钮直接在你的单元格上,你的问题与这一行有关let cell = sender.superview?.superview as? SocialCellView
你正在添加一个额外的超级视图,所以你需要修改你的代码到这个,正如我在我的之前的评论
@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)
}
}
希望对您有所帮助
我有一个包含文本和图像的 UITableView。我可以与图像共享文本,但由于 nil 值,我会崩溃。图片已经下载,但我似乎无法让它们工作这是我的代码
@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)
}
}
在上面的代码中,我在 Cell 和 myImage 上也得到了 nil。图像再次显示在 TableView 上,但似乎无法让它们与 UIActivityViewController 一起工作。
根据你的评论,你的按钮直接在你的单元格上,你的问题与这一行有关let cell = sender.superview?.superview as? SocialCellView
你正在添加一个额外的超级视图,所以你需要修改你的代码到这个,正如我在我的之前的评论
@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)
}
}
希望对您有所帮助