ios - 如何在不重新加载页面的情况下刷新 Swift 中的 ImageView ?

标签 ios swift

我有一个连接到 facebookgraphAPI 并下载当前用户个人资料图片的更新照片按钮。 我希望在不重新加载 viewController 的情况下刷新 View 中的图像。 有什么办法可以做到这一点?我知道 tableviewreloadDatarefreshcontrol 但我没有使用 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/

相关文章:

arrays - 你将如何在 Swift 中创建一个具有 n 维的多维数组?

ios - 找不到这样的模块 'GoogleMaps' 但是已安装

ios - 在 podfile 错误中更新 Alamofire

iphone - 在 iOS 应用程序中跟踪用户指标?

ios - 使类类型成为字典键(Equatable,Hashable)

ios - 快速检查数组元素

ios - 有没有办法使用 swift 将数组保存在数组中?

iphone - 不同的 REST 客户端 = 不同的响应

ios - ColorSense-For-XCode 插件在 swift 代码中不起作用

ios - 用户壁纸作为背景