从 Facebook 获取个人资料图片太慢
getting profile picture from Facebook too slow
我正在尝试从 Facebook 获取用户的个人资料图片,以将其显示在他的 Account
屏幕上。它有效,但问题是,它太慢了。屏幕将加载,半秒后,个人资料图片加载。这是我的 viewDidLoad
代码,我在其中发出 graph API
请求:
override func viewDidLoad() {
super.viewDidLoad()
currentUser = User.CurrentUser
editProfileButton.layer.cornerRadius = 2.5
// checking for logged in user
let defaults = NSUserDefaults.standardUserDefaults()
if FBSDKAccessToken.currentAccessToken() != nil {
// logged in through facebook, do nothing
updateCurrentUser()
} else {
// log in
presentLoginViewController()
}
userNameLabel.text = currentUser.first_name + " " + currentUser.last_name
userPhoneLabel.text = currentUser.phone_number
//getting profile picture: too slow for now.
if FBSDKAccessToken.currentAccessToken().hasGranted("user_photos") {
let userId = FBSDKProfile.currentProfile().userID
let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"picture.type(large)"])
graphRequest.startWithCompletionHandler({(connection: FBSDKGraphRequestConnection!, response: AnyObject?, error: NSError!) in
if error != nil {
// error
}
if let resultDic = response as? NSDictionary {
let data = resultDic["picture"] as? NSDictionary
let dataDict = data!["data"] as? NSDictionary
let imageStringUrl = dataDict!["url"] as? String
let imageUrl = NSURL(string: imageStringUrl!)
let imageData = NSData(contentsOfURL: imageUrl!)
self.currentUser.profilePicture = UIImage(data: imageData!)
self.userPicImageView.image = self.currentUser.profilePicture
}
})
}
// user profile picture set up
userPicImageView.clipsToBounds = true
userPicImageView.contentMode = .ScaleAspectFill
userPicImageView.layer.cornerRadius = userPicImageView.frame.height / 2.67
}
我可以让它更快吗?
对于初学者来说,将 FBSDKGraphRequest
调用放在生命周期的后面绝对不会帮助您更快地获取数据。 viewDidLoad
是放置它的好地方 - 如果您必须在特定屏幕内启动它。
接下来,异步抓取图片有助于提高用户体验。您当前拥有的代码将阻塞主线程,因为下载过程是在主线程上进行的。我建议您阅读 NSURLSession
以处理获取图像的问题。
快速获取照片的技巧
有几种可靠的方法可以获取您的照片 "faster"。一种方法是抓取较小的图像(如果可以的话)。
如果你必须有一个大图像,一个好的方法是在另一个class中抽象网络调用,并尽可能早地初始化过程,例如在didFinishLaunchingWithOptions
方法中AppDelegate 的。
例如,你可以有一个 class:
class FacebookDetails {
static let sharedInstance = FacebookDetails()
var image: UIImage?
private init() {
populateImage()
}
func populateImage() {
let graphRequest = FBSDKGraphRequest... //continue with populating image
}
}
现在您可以在应用程序生命周期中尽早初始化此 class:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FacebookDetails.sharedInstance
return true
}
当您到达需要图像的视图控制器时,调用 FacebookDetails.sharedInstance.image
以检索已加载的图像(图像加载时间仍受网络强度影响),但这是一种开始加载图像的方法在您的应用程序生命周期中尽早。
我正在尝试从 Facebook 获取用户的个人资料图片,以将其显示在他的 Account
屏幕上。它有效,但问题是,它太慢了。屏幕将加载,半秒后,个人资料图片加载。这是我的 viewDidLoad
代码,我在其中发出 graph API
请求:
override func viewDidLoad() {
super.viewDidLoad()
currentUser = User.CurrentUser
editProfileButton.layer.cornerRadius = 2.5
// checking for logged in user
let defaults = NSUserDefaults.standardUserDefaults()
if FBSDKAccessToken.currentAccessToken() != nil {
// logged in through facebook, do nothing
updateCurrentUser()
} else {
// log in
presentLoginViewController()
}
userNameLabel.text = currentUser.first_name + " " + currentUser.last_name
userPhoneLabel.text = currentUser.phone_number
//getting profile picture: too slow for now.
if FBSDKAccessToken.currentAccessToken().hasGranted("user_photos") {
let userId = FBSDKProfile.currentProfile().userID
let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"picture.type(large)"])
graphRequest.startWithCompletionHandler({(connection: FBSDKGraphRequestConnection!, response: AnyObject?, error: NSError!) in
if error != nil {
// error
}
if let resultDic = response as? NSDictionary {
let data = resultDic["picture"] as? NSDictionary
let dataDict = data!["data"] as? NSDictionary
let imageStringUrl = dataDict!["url"] as? String
let imageUrl = NSURL(string: imageStringUrl!)
let imageData = NSData(contentsOfURL: imageUrl!)
self.currentUser.profilePicture = UIImage(data: imageData!)
self.userPicImageView.image = self.currentUser.profilePicture
}
})
}
// user profile picture set up
userPicImageView.clipsToBounds = true
userPicImageView.contentMode = .ScaleAspectFill
userPicImageView.layer.cornerRadius = userPicImageView.frame.height / 2.67
}
我可以让它更快吗?
对于初学者来说,将 FBSDKGraphRequest
调用放在生命周期的后面绝对不会帮助您更快地获取数据。 viewDidLoad
是放置它的好地方 - 如果您必须在特定屏幕内启动它。
接下来,异步抓取图片有助于提高用户体验。您当前拥有的代码将阻塞主线程,因为下载过程是在主线程上进行的。我建议您阅读 NSURLSession
以处理获取图像的问题。
快速获取照片的技巧
有几种可靠的方法可以获取您的照片 "faster"。一种方法是抓取较小的图像(如果可以的话)。
如果你必须有一个大图像,一个好的方法是在另一个class中抽象网络调用,并尽可能早地初始化过程,例如在didFinishLaunchingWithOptions
方法中AppDelegate 的。
例如,你可以有一个 class:
class FacebookDetails {
static let sharedInstance = FacebookDetails()
var image: UIImage?
private init() {
populateImage()
}
func populateImage() {
let graphRequest = FBSDKGraphRequest... //continue with populating image
}
}
现在您可以在应用程序生命周期中尽早初始化此 class:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FacebookDetails.sharedInstance
return true
}
当您到达需要图像的视图控制器时,调用 FacebookDetails.sharedInstance.image
以检索已加载的图像(图像加载时间仍受网络强度影响),但这是一种开始加载图像的方法在您的应用程序生命周期中尽早。