使用框架将图像分享到社交媒体

时间:2016-04-20 13:19:54

标签: ios swift

我要将编辑过的图片分享给社交形象。目前,我使用0.png作为输出图像。

现在,如何将0.png替换为已编辑的图像并进行共享?

import UIKit
import Social

class EditController: UIViewController {
var image:UIImage?
var index:Int?

@IBOutlet weak var photo: UIImageView!

let context = CIContext(options: nil)

override func viewDidLoad() {
    super.viewDidLoad()
    self.photo.image = self.image
}

@IBAction func applyFilter(sender: UIBarButtonItem) {
    print("applyFilter")
    let beginImage = CIImage(image: self.image!)

    let randomColor = [kCIInputAngleKey: (Double(arc4random_uniform(314)) / 100)]
    let filteredImage = beginImage!.imageByApplyingFilter("CIHueAdjust", withInputParameters: randomColor)
    let renderedImage = context.createCGImage(filteredImage, fromRect: filteredImage.extent)
    self.image = UIImage(CGImage: renderedImage)
    self.photo.image = self.image
}

@IBAction func shareToFacebook(sender: AnyObject) {
    let shareToFacebook : SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
    shareToFacebook.setInitialText("Hello I Posted Using Swift And Social Framework")
    shareToFacebook.addImage(UIImage(named: "0.png"))
    self.presentViewController(shareToFacebook, animated: true, completion: nil)
}
}

2 个答案:

答案 0 :(得分:0)

在迅捷中 只需创建一个具有SLComposeViewController方法的按钮 在项目支持的文件中获取任何随机图片,例如" 32.png" :

 @IBAction func shareToTwitter() {
        var shareToTwitter : SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
        shareToTwitter.setInitialText("Hello World")
        shareToTwitter.addImage(UIImage(named: "32.png"))
        self.presentViewController(shareToTwitter, animated: true, completion: nil)
    }

信用:

更多教程:

答案 1 :(得分:0)

OP在其提问的评论中回答:

  

我通过将UIImage(named: "0.png")替换为self.image来修复它。