无法使用 swift 在 Facebook 上发布 QR 码图像
Posted
技术标签:
【中文标题】无法使用 swift 在 Facebook 上发布 QR 码图像【英文标题】:Unable to post QR Code Image on Facebook with swift 【发布时间】:2015-03-04 11:23:22 【问题描述】:我正在创建一个二维码阅读器应用程序,一旦创建了二维码图像,用户就可以将其发布到 Facebook 上。这是我的代码:
import UIKit
import Social
import MobileCoreServices
class NextViewController: UIViewController
var userTextString : String?
@IBOutlet weak var QRCodeImage: UIImageView!
var tasteImage = UIImage(named: "Back.jpg")
@IBOutlet weak var userText: UILabel!
override func viewDidLoad()
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Share", style: UIBarButtonItemStyle.Bordered, target: self, action: "ShareIt")
self.userText.text = self.userTextString
var qrText = self.userTextString
CGSize(width: 300, height: 300)
self.QRCodeImage.image =
var qrCode = QRCode("\(qrText)")!
qrCode.size = self.QRCodeImage.bounds.size
return qrCode.image
()
func ShareIt()
var activityIteams : [AnyObject]?
if QRCodeImage.image != nil
activityIteams = [userText.text as String!, self.QRCodeImage.image as UIImage!]
else
activityIteams = [userText.text as String!]
println(activityIteams)
let activityController = UIActivityViewController(activityItems: activityIteams!, applicationActivities: nil)
self.presentViewController(activityController, animated: true, completion: nil)
但我无法使用此代码在 Facebook 上发布任何内容,如果我尝试仅发布文本,则它可以工作,但图像无法正常工作,我在控制台中得到了这个:
2015-03-04 16:45:18.128 testing[11389:160922] plugin com.apple.share.Facebook.post interrupted
如果我尝试另一个已经存储到项目中的图片,我可以轻松地将其发布到 facebook 但我不知道我的二维码图片有什么问题。
这是我的示例项目以获取更多信息。
https://github.com/DharmeshKheni/QR-Code
【问题讨论】:
【参考方案1】:我得到了类似这样的解决方案:
首先我将图像保存到文档目录,然后从该目录中检索该图像,这对我来说很好用是我的更新代码。
import UIKit
import Social
import MobileCoreServices
class QRImage: UIViewController
var userTextString : String?
@IBOutlet weak var QRCodeImage: UIImageView!
@IBOutlet weak var userText: UILabel!
var imageView = UIImageView()
override func viewDidLoad()
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Share", style: UIBarButtonItemStyle.Bordered, target: self, action: "ShareIt")
self.userText.text = self.userTextString
var qrText = self.userTextString
CGSize(width: 300, height: 300)
self.QRCodeImage.image =
var qrCode = QRCode("\(qrText)")!
qrCode.size = self.QRCodeImage.bounds.size
return qrCode.image
()
self.createThumBil()
func ShareIt()
var activityIteams : [AnyObject]?
if QRCodeImage.image != nil
activityIteams = [userText.text as String!, self.imageView.image as UIImage!]
else
activityIteams = [userText.text as String!]
println(activityIteams)
let activityController = UIActivityViewController(activityItems: activityIteams!, applicationActivities: nil)
self.presentViewController(activityController, animated: true, completion: nil)
func getImage()
var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let getImagePath = paths.stringByAppendingPathComponent("QRCode.png")
self.imageView.image = UIImage(contentsOfFile: getImagePath)
func createThumBil()
let originalImage : UIImage = self.QRCodeImage.image!
let destinationSize : CGSize = CGSizeMake(200, 200)
UIGraphicsBeginImageContext(destinationSize)
originalImage.drawInRect(CGRectMake(0, 0, destinationSize.width, destinationSize.height))
let newImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()
let thumbNailimageData : NSData = UIImagePNGRepresentation(newImage)
UIGraphicsEndImageContext()
if let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
if paths.count > 0
if let dirPath = paths[0] as? String
println(dirPath)
thumbNailimageData.writeToFile(dirPath.stringByAppendingPathComponent("QRCode.png"), atomically: true)
self.getImage()
【讨论】:
以上是关于无法使用 swift 在 Facebook 上发布 QR 码图像的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 Facebook SDK IOS Swift 登录 [重复]
无法使用 Swift 在 Xcode 中使用 Parse 1.7.2 和 Facebook SDK 4.1 创建新用户
swift:Xcode无法为Facebook框架生成Swift表示