加载用户图像

时间:2016-01-10 05:40:16

标签: swift parse-platform uiimageview

当用户搜索其他用户时,我使用此代码加载用户用户名

var user: PFUser? {

        didSet {
            userNameLabel.text = user?.username
        }

    }

如何为个人资料照片做同样的事情?

2 个答案:

答案 0 :(得分:0)

这是你可以做的。

案例1

如果您需要下载图像并显示 -

  1. 创建一个包含图片网址的属性。
  2. 异步下载图片并显示
  3. 所以代码看起来就是这个

    var pictureURL: NSURL? 
    var user: PFUser? {
    
            didSet {
                userNameLabel.text = user?.username
                pictureURL = user?.pictureURL
                displayPicture()
            }
    
        }
    
        private func displayPicture() {
        // Async download pic and display
        }
    

    案例2 如果您在本地拥有图像,则只显示 -

    var user: PFUser? {
    
            didSet {
                userNameLabel.text = user?.username
                userImage.image = UIImage(named: "Picturename")
            }
    
        } 
    

答案 1 :(得分:0)

无法 Parse.com 上以UIImage格式保存您的图片文件。您可以使用PFFile存储文件,并且您必须图像转换为PFFile,以便保存并获取图像。

您可以使用此功能保存图像;

 private func saveLogo() {
    let yourLogoImageFile = UIImage(named: "YourLogoFileName")
    if let imageData = UIImageJPEGRepresentation(yourLogoImageFile!, 0.1) {
        // You got the PFFile you can use this for save.
        let imagePFFile = PFFile(name: "logo.png", data: imageData)
        logo = imagePFFile // Send this PFFile to your variable or just save.
        // Saving...
        imagePFFile?.saveInBackground()
    }
}

之后,您可以从Parse获取徽标PFFile,并且必须使用此功能将其转换为UIImage;

    private func getLogo() {
    if let image = yourLogoVariable {
        image.getDataInBackgroundWithBlock() {
            (let imageData, error) in
            if error == nil {
                if let logoImageData = imageData {
                    if let logoImage = UIImage(data: logoImageData) {
                        // logoImage is the UIImage you can use this for your UIImageView.
                    }
                }
            }
        }
    }
}