2015-06-16 67 views
0

我正嘗試使用swift顯示來自parse的圖像。這是我的代碼:嘗試使用Swift顯示來自Parse的圖像時出現問題

var query = PFQuery(className: "Maps") 
      query.getObjectInBackgroundWithId("1234asdf3456") { 
       (object: PFObject?, error: NSError?) -> Void in 
       if error == nil 
       { 
        println(object) 
        var objectAsPF = object as PFObject! 
        let file = objectAsPF["imageFile"] as! PFFile 
        file.getDataInBackgroundWithBlock { 
         (imageData:NSData?, error:NSError?) -> Void in 
         if error == nil { 
          if let imageData = imageData { 
           let map:UIImage = UIImage(data: imageData)! 
           self.MapView.image = map 
           println("success") 
          } 
         } 
        } 

       } 
       else 
       { 
        println(error) 
       } 
      } 

我在的println(「成功」)中設置一個斷點,我檢查變量值,一切都很好,直到我試圖爲imageData轉換成UIImage的。有小費嗎?

+0

什麼錯誤以及你想轉換? –

+0

刪除此行'if let imageData = imageData'和{爲那個。 –

+0

圖像沒有出現錯誤,圖像沒有出現在屏幕上。 MapView是一個圖像視圖,我嘗試使用UIImage(data:imageData)將NSData轉換爲UIImage。當我在代碼中設置斷點時,我發現NSData不是零,但圖像是零。 – ANovice

回答

0

使用此代碼來解析retreave圖像然後將其從PFFile轉換爲一個UIImage ...

 var query = PFQuery(className:"Maps") 

      query.findObjectsInBackgroundWithBlock { 
       (objects: [AnyObject]?, error: NSError?) -> Void in 

       if error == nil { 
        // The find succeeded. 
        self.scored = objects!.count 
        // Do something with the found objects 
        if let objects = objects as? [PFObject] { 
         for object in objects { 




          let userImageFile = object["imageFile"] as! PFFile 
          userImageFile.getDataInBackgroundWithBlock { 
           (imageData: NSData?, error: NSError?) -> Void in 
           if error == nil { 
            if let imageData = imageData { 
             let image = UIImage(data:imageData) 

            if image != nil { 
             self.imageArray.append(image!) 
            } 

            } 
           } 




          } 





         } 
        } 
       } else { 
        // Log details of the failure 
        println("Error: \(error!) \(error!.userInfo!)") 
       } 

       dispatch_async(dispatch_get_main_queue()) { 

        println("Finished Loading Image") 



       } 
+0

嗨,關於此代碼的幾個問題。 self.scored的目的是什麼?此外dispatch_async(dispatch_get_main_queue()) – ANovice

+0

的目的也是當我運行此代碼時,我得到一個致命錯誤:意外地發現零,同時展開一個可選的值 – ANovice

+0

查看代碼中更新的答案。 –

相關問題