简体   繁体   English

Swift Facebook分享UIImageview图片

[英]Swift Facebook share UIImageview image

I would like to make Facebook share button on my app that shares the image that is loaded from the UIImageview. 我想在我的应用程序上设置Facebook共享按钮,以共享从UIImageview加载的图像。 I cannot figure out how to do it.. There is option to add the text on Facebook share window, but can't add the image. 我不知道该怎么办。。可以在Facebook共享窗口上添加文本,但不能添加图像。 Please help. 请帮忙。

import UIKit
import Social

class ViewController: UIViewController {


    @IBOutlet weak var saveButtonVar: UIButton!
    @IBOutlet weak var image: UIImageView!

    @IBAction func facebookBtn(sender: AnyObject) {
        var facebookBtn : SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
            facebookBtn.setInitialText("I am sharing my motivation with you!")
            facebookBtn.addImage(UIImage(named: "motImg")) //????
        self.presentViewController(facebookBtn, animated: true, completion: nil)

    }

    @IBAction func twitterBtn(sender: AnyObject) {

    }





    @IBAction func saveButton(sender: AnyObject) {

        UIImageWriteToSavedPhotosAlbum(image.image, nil, nil, nil)

        let alertController = UIAlertController(title: "Image Saved!", message:
            "", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,handler: nil))

        self.presentViewController(alertController, animated: true, completion: nil)


    }




    @IBAction func button(sender: AnyObject) {

        load_image("https://dl.dropboxusercontent.com/u/26347781/Images/Image\(arc4random_uniform(17) + 1).jpg")

        saveButtonVar.hidden = false


    }

    func load_image(urlString:String)
    {

        var imgURL: NSURL = NSURL(string: urlString)!
        let request: NSURLRequest = NSURLRequest(URL: imgURL)
        NSURLConnection.sendAsynchronousRequest(
            request, queue: NSOperationQueue.mainQueue(),
            completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
                if error == nil {
                    self.image.image = UIImage(data: data)
                }
        })

    }


    override func viewDidLoad() {

        super.viewDidLoad()

        saveButtonVar.hidden = true

        image.image = UIImage(named: "motImg")



    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



}

Swift 4 斯威夫特4

Integrate FacebookShare sdk. 集成FacebookShare SDK。

People can share photos from your app to Facebook with the Share Dialog or with a custom interface: 人们可以使用“共享对话框”或自定义界面将您的应用程序中的照片共享到Facebook:

-Photos must be less than 12MB in size. -照片大小必须小于12MB。

-People need the native Facebook for iOS app installed, version 7.0 or higher -人们需要安装适用于iOS应用程序(版本7.0或更高版本)的本地Facebook

@IBAction func facebookBtn(sender: AnyObject) {
  let shareImage = SharePhoto()
  shareImage.image = imageView.image //Image from your imageview
  shareImage.isUserGenerated = true

  let content = SharePhotoContent()
  content.photos = [shareImage]

  let sharedDialoge = ShareDialog()
  sharedDialoge.shareContent = content

  sharedDialoge.fromViewController = self
  sharedDialoge.mode = .automatic


  if(sharedDialoge.canShow)
  {
    sharedDialoge.show()
  }
  else
  {
    print("Install Facebook client app to share image")
  }
}

Hei there! 嘿!

Check out this guy's answer: Swift UIActivityViewController Image&Url Share not working with FB 看看这个家伙的答案: Swift UIActivityViewController Image&Url Share与FB不兼容

For many people it seemed to work the example in the link, so maybe it does for you, too. 对于许多人来说,似乎可以使用链接中的示例,因此也许对您也有用。 However, so far it did not work for me and I came across your question while still investigating my problem. 但是,到目前为止,它对我没有用,我在调查我的问题的同时遇到了您的问题。

As far as adding text, I know this functionality is off for some time, but you should be able to add an url link. 至于添加文本,我知道此功能已经关闭了一段时间,但是您应该能够添加url链接。

Let me know how this works for you. 让我知道这对您如何工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM