如何使用 Swift 3 从 Facebook SDK 下载照片?
Posted
技术标签:
【中文标题】如何使用 Swift 3 从 Facebook SDK 下载照片?【英文标题】:How to download photo from Facebook SDK with Swift 3? 【发布时间】:2016-10-04 10:53:22 【问题描述】:在 ios 应用中下载用户个人资料照片时出现崩溃:
let dataPhoto = try! Data(contentsOf: photoURL, options: .mappedIfSafe)
(lldb) po photoURL
▿ https:/scontent.xx.fbcdn.net/v/t1.0-1/p200x200/13418946_10208553701714177_3969269576626117653_n.jpg%3Foh=add39246ec9693ecead0529ecbbbfc53&oe=5862D7ED -- file:///
fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=260 "The file “13418946_10208553701714177_3969269576626117653_n.jpg?oh=add39246ec9693ecead0529ecbbbfc53&oe=5862D7ED” couldn’t be opened because there is no such file." UserInfo=NSFilePath=/https:/scontent.xx.fbcdn.net/v/t1.0-1/p200x200/13418946_10208553701714177_3969269576626117653_n.jpg?oh=add39246ec9693ecead0529ecbbbfc53&oe=5862D7ED, NSUnderlyingError=0x170246000 Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory": file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.46.2/src/swift/stdlib/public/core/ErrorType.swift, line 178
2016-10-04 12:48:14.317003 Movie Buffs[2722:1318691] fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=260 "The file “13418946_10208553701714177_3969269576626117653_n.jpg?oh=add39246ec9693ecead0529ecbbbfc53&oe=5862D7ED” couldn’t be opened because there is no such file." UserInfo=NSFilePath=/https:/scontent.xx.fbcdn.net/v/t1.0-1/p200x200/13418946_10208553701714177_3969269576626117653_n.jpg?oh=add39246ec9693ecead0529ecbbbfc53&oe=5862D7ED, NSUnderlyingError=0x170246000 Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory": file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.46.2/src/swift/stdlib/public/core/ErrorType.swift, line 178
发生了什么事?在早期的 Swift 中它可以工作。
使用this 方法得到相同的结果:
URLSession.shared.dataTask(with: photoURL) (dataPhoto, response, error) in
【问题讨论】:
【参考方案1】:代替
URL(fileURLWithPath: profilePictureURLStr)
需要像这样构建URL:
let photoURL = URL(string: profilePictureURLStr)!
【讨论】:
以上是关于如何使用 Swift 3 从 Facebook SDK 下载照片?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Swift 3 中获取 Facebook 用户 ID、电子邮件、图片类型(大)
如何从 Facebook + Swift + Parse 获取封面照片