Swift Facebook share UIImageview image

╄→尐↘猪︶ㄣ 提交于 2021-02-11 06:09:14

问题


I would like to make Facebook share button on my app that shares the image that is loaded from the 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. 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.
    }



}

回答1:


Swift 4

Integrate FacebookShare sdk.

People can share photos from your app to Facebook with the Share Dialog or with a custom interface:

-Photos must be less than 12MB in size.

-People need the native Facebook for iOS app installed, version 7.0 or higher

@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")
  }
}



回答2:


Hei there!

Check out this guy's answer: Swift UIActivityViewController Image&Url Share not working with 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.

Let me know how this works for you.



来源:https://stackoverflow.com/questions/30812602/swift-facebook-share-uiimageview-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!