【发布时间】:2015-04-30 22:13:30
【问题描述】:
我收到以下异常:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[__NSCFDictionary imagePathForPictureMode:size:]:无法识别的选择器发送到实例 0x7ff9f8d25d10'
使用此代码:
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions, block: {
(user: PFUser?, error: NSError?) -> Void in
if let user = user {
if user.isNew {
var request = FBSDKGraphRequest(graphPath: "me", parameters: nil)
request.startWithCompletionHandler({
(connection, result, error) -> Void in
if error == nil {
// exception thrown next line
let strProfilePicURL = result.imagePathForPictureMode(FBSDKProfilePictureMode.Square, size: self.profileImage.frame.size)
let url = NSURL(string: strProfilePicURL, relativeToURL: NSURL(string: "http://graph.facebook.com/"))
let imageData = NSData(contentsOfURL: url!)
let image = UIImage(data: imageData!)
self.profileImage.image = image!
}
})
println("User signed up and logged in through Facebook!")
} else {
println("User logged in through Facebook!")
}
} else {
println("Uh oh. The user cancelled the Facebook login.")
}
})
为什么会在这里抛出异常?我也试过:
let size = self.profileImage.frame.size as CGSize
let strProfilePicURL = result.imagePathForPictureMode(FBSDKProfilePictureMode.Square, size: size)
【问题讨论】:
-
主要是因为图形请求(如/me)的结果不是FBSDKProfile。
-
@MingLi 我的错!我认为这是因为代码完成自动建议
imagePathForPictureMode...我已经尝试过FBSDKProfile.currenProfile(),但它似乎总是为零。任何想法为什么? -
您是否已经拥有有效的访问令牌? “currentProfile”是在 SDK 获取访问令牌后异步获取的,因此取决于您何时请求 currentProfile,它可能为 nil。您还可以观察更新配置文件时的 FBSDKProfileDidChangeNotification。见developers.facebook.com/docs/reference/ios/current/class/…
-
@MingLi 我采纳了你的建议,沿着
FBSDKProfileDidChangeNotification路线走下去,现在运行良好。谢谢您的帮助。您可以发布一个答案让我接受。
标签: swift facebook-graph-api facebook-ios-sdk