从parse.com加载图像 [英] Loading images from parse.com

查看:91
本文介绍了从parse.com加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要解析的保存图像:

This is my saving image to parse:

func uploadPost(){
        var imageText = self.imageText.text

        if (imageView.image == nil){
            println("No image uploaded")
        }
        else{
            var posts = PFObject(className: "Posts")
            posts["imageText"] = imageText
            posts["uploader"] = PFUser.currentUser()
            posts.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
                if error == nil{
                    //**Success saving, now save image.**//

                    // Create an image data
                    var imageData = UIImagePNGRepresentation(self.imageView.image)
                    // Create a parse file to store in cloud
                    var parseImageFile = PFFile(name: "upload_image.png", data: imageData)
                    posts["imageFile"] = parseImageFile
                    posts.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
                        if error == nil{
                            // Take user home
                            println("Data uploaded")
                        }
                        else{
                            println(error)
                        }
                    })
                }
                else{
                    println(error)
                }
            })
        }
    }

如何从解析中加载图像?这就是我的Parse.com帖子"数据的样子:

How can I load the images from parse? This is how my Parse.com "Posts" data looks like:

有什么建议吗?

我认为也许是这样的:

self.imageView.sd_setImageWithURL(url, completed: block)

但是我不知道如何获取URL.如果图像名称不同怎么办?

But I don´t know how I get the URL. And what if the images has different names?

推荐答案

尝试类似的方法

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

其中pfObject是对对象的引用.还有其他方法可以检查nil值,但这应该可以工作.

Where pfObject is the reference to your object. There are other ways you could check for a nil value, but this should work.

只要您有对正确对象的引用(可以通过查询objectId来获得引用),则只需要存储图像文件所在列的名称,而不需要图像文件本身.

As long as you have a reference to the correct object (which you can get via a query for objectId) then you should only need the name of the column the image is file is stored in and not the image file itself.

这篇关于从parse.com加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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