2017-10-17 71 views
0

我需要帮助附加从firebase下载的图像,以便我可以使用它来共享UIActivityViewController。我得到正确的图像,但不确定如何设置数据传递以让它共享。当前设置在“objectsToShare = [self.image!]”上引发“线程1错误”。Swift Firebase为UIActivityViewController附加图片

let shareAction = UIAlertAction(title: "Share", style: UIAlertActionStyle.default, handler: {(alert: UIAlertAction!) in 
     var objectsToShare: [AnyObject]? 


     let titlePost = self.feeds[sender.tag].downloadURL 

     if let postURL = URL(string: titlePost) { 
      let postRequest = URLRequest(url: postURL) 
      self.image?.setImageWith(postURL, placeholderImage: nil, options: SDWebImageOptions.progressiveDownload, completed: { (imageRequest, imageResponse, error) -> Void in 
       // failure downloading image 
       print("Error downloading Firebase post image") 
       print(error) 
      }) 
     } 

     objectsToShare = [self.image!] 
     let activityViewController = UIActivityViewController(activityItems: objectsToShare!, applicationActivities: nil) 

     // present the view controller 
     self.present(activityViewController, animated: true, completion: nil) 
    }) 

回答

0

在地方的下面:

objectsToShare = [self.image!] 

你必须使用这样的:

if let image = self.image { 
     objectsToShare.append(image) 
} 
+0

获得“objectsToShare”为零。也许我没有正确下载图像? –

+0

需要一些时间才能正确加载图像。当你分配图像时,可能不是下载。因此,将该代码移至完成块,然后对其进行测试。 –

0

您应该检查完成处理程序是第一的。

您应该检查完成时是否有错误,如果没有,请执行您的图像处理。由于请求是异步的,图像还没有在线上。 做那样的事:

self.image?.setImageWith(postURL, placeholderImage: nil, options: SDWebImageOptions.progressiveDownload, completed: { (imageRequest, imageResponse, error) -> Void in 
    // This closure is called when the request is done 
    if error == nil { 
     objectsToShare = [self.image!] 
     let activityViewController = UIActivityViewController(activityItems: objectsToShare!, applicationActivities: nil) 
     // present the view controller 
     self.present(activityViewController, animated: true, completion: nil) 
    } else { 
     // failure downloading image 
     print("Error downloading Firebase post image") 
     print(error) 
    } 
}) 
+0

你可能想重新检查你的答案。很多错误。 –

+0

现在检查一下,我没有复制2行。如果还有错误,它来自您的代码,因为我只复制粘贴并重新排列它。我解释了你应该做什么,所以即使对于初学者开发人员也应该足够了 –

+0

我得到这个警告“if error == nil”“比较类型'SDImageCacheType'的非可选值总是返回false” –

相关问题