我有一个连接到 facebookgraphAPI 并下载当前用户个人资料图片的更新照片按钮。
我希望在不重新加载 viewController
的情况下刷新 View 中的图像。
有什么办法可以做到这一点?我知道 tableview
有 reloadData
和 refreshcontrol
但我没有使用 tableview
。
class ProfileViewController: UIViewController , UITextViewDelegate{
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var profileTextView: UITextView!
@IBAction func updatePicture(sender: UIButton) {
// pulls in latest facebook profile info
let vc = ViewController()
vc.getFBUserInfo(PFUser.currentUser()!)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.imageView.setNeedsDisplay()
self.loadPhoto()
})
}
override func viewDidLoad() {
super.viewDidLoad()
loadProfile()
loadPhoto()
}
func loadPhoto() {
currentUser()?.getPhoto({
image in
self.imageView.layer.masksToBounds = true
self.imageView.contentMode = .Center
self.imageView.image = image
})
}
func loadProfile () {
nameLabel.text = currentUser()?.name
// get the existing profile from DB
if let profile = PFUser.currentUser()!.objectForKey("profileText") as? String {
profileTextView.text = profile
}
profileTextView.delegate = self
}
User.swift getPhoto 函数:
func getPhoto(callback:(UIImage) -> ()) {
let imageFile = pfUser.objectForKey("picture") as! PFFile
imageFile.getDataInBackgroundWithBlock({
data, error in
if let data = data {
callback(UIImage(data: data)!)
}
})
}
最佳答案
您的回调看起来可能是在后台队列中调用的。当您在后台访问它的类时,UIKit 不喜欢它,一个常见的副作用是它看起来像 UI 不更新(或直到延迟后才更新)。
尝试
func loadPhoto() {
currentUser()?.getPhoto({
image in
dispatch_async(dispatch_get_main_queue(), {
self.imageView.layer.masksToBounds = true
self.imageView.contentMode = .Center
self.imageView.image = image
})
})
}
关于ios - 如何在不重新加载页面的情况下刷新 Swift 中的 ImageView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34802575/