尝试使用Swift从Parse显示图像时出现问题 [英] Problems When Trying to display images from Parse using Swift

查看:112
本文介绍了尝试使用Swift从Parse显示图像时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用swift显示来自解析的图像.这是我的代码:

I am trying to display an image from parse using swift. This is my code:

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("success")处设置了一个断点,并检查了变量值,一切都很好,直到我尝试将imageData转换为UIImage为止.有提示吗?

I set a breakpoint at println("success") and i checked the variable values and everything is fine until i try to convert imageData to UIImage. Any tips?

推荐答案

使用此代码从解析中撤消图像,然后将其从PFFile转换为UIImage ...

Use this code to retreave images from parse then convert it from a PFFile to a 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")



                }

这篇关于尝试使用Swift从Parse显示图像时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆