2015-06-11 15 views
2

在我的應用程序中,當用戶註冊時,他/她註冊,圖像被添加到用戶類中。用於執行此操作的代碼是...如何從用戶類分析中提取文件swift

var newUser = PFUser() 
    let imageData = UIImagePNGRepresentation(self.imageView.image) 
    let imageFile = PFFile(data: imageData) 
    newUser.setObject(imageFile, forKey: "image") 
    newUser.signUpInBackgroundWithBlock({ 
     (success, error) -> Void in 
    }) 

後來在我的應用程序中,我想將該圖片放入UIImageView中。我試圖做到這一點的方式是這樣的。

var user = PFUser.currentUser() //Error on this line 
    let profileImage = user["image"] as! PFFile 

但是,這會返回錯誤「AnyObject?不能轉換爲PFFile」。我想知道如何使用用戶類中的關鍵字「image」檢索文件。謝謝你的幫助。

+0

PFFile線上的錯誤?或者在用戶= PFUser.currentUser()行? – Jbryson

+0

PFUser.currentUser()行@Jbryson –

回答

1

圖像作爲數據文件存儲在解析中。從解析中再次檢索圖像,並將其加載到您的UIImage。您需要將圖像

var user = PFUser.currentUser() 
let userImageFile = user["image"] as PFFile 
userImageFile.getDataInBackgroundWithBlock { 
(imageData: NSData!, error: NSError!) -> Void in 
if !error { 
    let image = UIImage(data:imageData) 
    } 
} 

轉換而改變let image您要加載圖像的圖像。

+0

我知道如何檢索它,但我的問題是我不知道如何訪問用戶類。行var user = PFUser.currentUser()不起作用。 @MrUziBazooka –

+0

@AidanKaiser嘗試將「var user = PFUser.currentUser()」更改爲「var user = PFUser()。currentUser」 – Icaro

+0

您可以從currentUser獲得任何信息,還是僅僅是圖像是問題? –