簡體   English   中英

使用UIActivityViewController將圖像和文本共享到Facebook Messenger失敗

[英]Sharing image and text to Facebook Messenger with UIActivityViewController failing

必須對下面的代碼進行哪些更改,以確保Messenger與UIActivityViewController很好地共享並共享圖像和文本,或者至少是圖像?


背景

我正在使用UIActivityViewController從我的應用程序共享文本和圖像,並將它們發送到電子郵件,消息和其他共享應用程序。 UIActivityViewController很棒,並且以大多數應用程序的簡單和標准方式工作......但是,我遇到了不想合作的Messenger(Facebook Messenger)的問題。

在下面的代碼中,我點擊一個UIButton ,它獲取屏幕的快照圖像,將其轉換為.png,然后將准備好的圖像( imageShare )和我准備好的文本( textShare )一起發送到UIActivityViewController 這個簡單的方法允許我的應用程序成功共享電子郵件,郵件和許多其他共享應用程序,除了Messenger。

(旁注,Facebook只能共享准備好的圖像( imageShare ),但不能共享文本。)


問題

嘗試與UIActivityViewController共享Messenger時,這些是問題:

  • 共享activityItems: [textShare, imageShare]

    • Messenger僅發送共享文本。
  • 共享activityItems: [textShare]

    • UIActivityViewController甚至不能使用共享到Messenger的選項。
  • 共享activityItems: [imageShare]

    • 顯示錯誤: “無法加載內容。 加載內容時出現問題。 請再試一次。”

@IBAction func myButton(sender: UIButton) {

    // Take snapshot of screen
    let imageSnapshot: UIImage!
    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 0)
    self.view.drawViewHierarchyInRect(CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height), afterScreenUpdates: false)
    imageSnapshot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    // Prepare text to share
    let textShare: String!
    textShare = "This is my original text."

    // Prepare image to share
    let imageShare: NSData
    imageShare = UIImagePNGRepresentation(imageSnapshot)!

    // Share text and image
    let activity = UIActivityViewController(activityItems: [textShare, imageShare], applicationActivities: nil)
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        self.presentViewController(activity, animated: true, completion: nil)
    }

}

圖片

iOS向UIActivityViewController展示了可用的應用程序。

UIActivityViewController截圖

嘗試使用UIImage而不是NSData。 您可以將UIImage轉換為AnyObject並添加到您的數組中。

另請看這個問題: 使用UIActivityViewController共享圖像

let textShare = "This is my original text."
var shareObject = [AnyObject]()
if textShare != nil {
    shareObject.append(textShare as AnyObject)
}

let Yourimage = UIImage(named: "image.png")
let imageData = UIImagePNGRepresentation(Yourimage!) as NSData? 

if let data = imageData {
    shareObject.append(data)
}

if shareObject.count > 0 { // check condition for text and image
    let activityViewController = UIActivityViewController(activityItems: shareObject, applicationActivities: nil)
    activityViewController.popoverPresentationController?.sourceView = self.view
    present(activityViewController, animated: true, completion: nil)
}

所以最終需要的是將NSData轉換回UIImage的額外步驟。

注意,這種將NSData轉換回UIImage方法的一個問題是它產生無損(即消息和郵件)或有損(即Notes,Photos,Messenger)的圖像質量。

有趣的是,消息,郵件和各種第三方共享應用程序非常滿意,在UIActivityViewController共享NSData ,而Messenger則不會容忍它。 (我有興趣明白為什么到底?)


變化

由此:

    // Prepare image to share
    let imageShare: NSData
    imageShare = UIImagePNGRepresentation(imageSnapshot)!

為此:

    // Prepare image to share
    let imageShareData: NSData
    imageShareData = UIImagePNGRepresentation(imageSnapshot)!
    let imageShare = UIImage(data: imageShareData)!

最終的工作代碼

@IBAction func myButton(sender: UIButton) {

    // Take snapshot of screen
    var imageSnapshot: UIImage!
    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 0)
    self.view.drawViewHierarchyInRect(CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height), afterScreenUpdates: false)
    imageSnapshot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    // Prepare text to share
    let textShare: String!
    textShare = "This is my original text."

    // Prepare image to share
    let imageShareData: NSData
    imageShareData = UIImagePNGRepresentation(imageSnapshot)!
    let imageShare = UIImage(data: imageShareData)!

    // Share text and image
    let activity = UIActivityViewController(activityItems: [textShare, imageShare], applicationActivities: nil)
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        self.presentViewController(activity, animated: true, completion: nil)
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM