gpt4 book ai didi

swift - 低分辨率图像加载时间过长

转载 作者:行者123 更新时间:2023-11-28 11:04:32 25 4
gpt4 key购买 nike

使用 Facebook Graph API,我检索到我想在 UIImageView 中显示的 200x200 个人资料图片的字符串 URL。我成功地做到了这一点,但我注意到图像显示在屏幕上可能需要长达 10 秒的时间。谁能给我一些关于如何优化它的建议(没有双关语意)?

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: self.profilePictureUrl)!, completionHandler: { (data, response, error) ->
Void in
self.profilePictureImageView.image = UIImage(data: data!)
self.profilePictureImageView.layer.cornerRadius = self.profilePictureImageView.frame.size.width / 2;
self.profilePictureImageView.clipsToBounds = true

dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.view.addSubview(self.profilePictureImageView)
})

}).resume()
}

最佳答案

您应该将所有 UIView 调用(因此您在 UIImageView 上设置的任何内容)移至主线程,因为 UIKit 大多数情况下都不是线程安全的。您可以在后台线程上实例化 UIImage 以优化性能,所以试试这个:

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

let url = NSURL(string: self.profilePictureUrl)!

NSURLSession.sharedSession().dataTaskWithURL(
url,
completionHandler: { [weak self] (data, response, error) -> Void in
guard let strongSelf = self else { return }

// create the UIImage on the background thread
let image = UIImage(data: data!)

// then jump to the main thread to modify your UIImageView
dispatch_async(dispatch_get_main_queue(), { [weak self] () -> Void in
guard let strongSelf = self else { return }

let profilePictureImageView = strongSelf.profilePictureImageView

profilePictureImageView.image = image
profilePictureImageView.layer.cornerRadius = profilePictureImageView.frame.size.width / 2;
profilePictureImageView.clipsToBounds = true

strongSelf.view.addSubview(profilePictureImageView)
})
}
).resume()
}

另请注意,我已-化了您对self 的引用。无法保证在调用完成例程时用户没有关闭正在启动此代码的 View Controller ,因此您要确保您没有保持对 self 的强引用。这允许 View Controller 在用户关闭它时解除分配,然后完成例程会提前返回而不做任何不必要的工作。

关于swift - 低分辨率图像加载时间过长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38921204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com