2017-10-17 68 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」 –

相關問題