2015-07-21 75 views
0

首先,簡單來說,如何將空白UIImage視圖更改爲我已經存儲在Parse中的圖像?這是到目前爲止我的代碼解析&迅速 - 隨機圖像

var query = PFQuery(className:"Content") 
    query.getObjectInBackgroundWithId("mlwVJLH7pa") { 
     (post: PFObject?, error: NSError?) -> Void in 
     if error == nil && post != nil { 

      //content.image = UIImage 

     } else { 
      println(error) 
     } 
    } 

在剛剛更換空白的UIImageView的頂部,請問有什麼可以做,它被替換爲隨機的形象呢?我認爲我不能再使用objectId了,因爲它特定於它所代表的那一行。

+0

什麼是稀疏? –

+0

不知道爲什麼我一直在說.... – Chrisicut

回答

0

首先,您需要使用query.findObjectsInBackgroundWithBlock而不是使用query.getObjectInBackgroundWithId

這會讓你看到所有的PFObjects。從它返回的數組中獲取一個隨機PFObject,現在您有一個隨機PFObject,您可以將其設置爲content.image

比方說,你PFObject具有「圖像」屬性作爲PFFile

簡單地做類似的東西以下轉換PFFile成一個UIImage(你應該使用存儲與解析圖像的東西。):

if let anImage = yourRandomPFObject["image"] as? PFFile { 
    anImage.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in 

    let image = UIImage(data:imageData) 
    content.image = image 
    } 
} 
1

我會先用getObjectsInBackgroundWithBlock從parse中檢索objectIds,然後從一個名爲objectId的變量中選擇一個隨機的objectId。這樣你可以節省用戶查詢每個對象的分析並使用大量的數據。

其次,我會

getObjectInBackgroundWithId(objectId) 
    if error == nil { 
     if let image: PFFile = objectRow["image"] as? PFFile{ 
     image.getDataInBackgroundWithBlock { 
     (imageData: NSObject?, error: NSError?) Void in -> 
      if let imageData = imageData { 
      let imageView = UIImageView(image: UIImage(data: imageData)) 
      } 
     } 
    } 

至少,這對我的作品。