从Parse在Swift中加载图像 [英] Loading image in Swift from Parse

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

问题描述

我已经成功地将数据从Parse提取为快速数据,但是我的图像似乎没有按照我的方式工作.

I'm successfully pulling in data from Parse into swift, but my images don't seem to be working the way I'm doing it.

在cellForRowAtIndexPath方法中,执行以下操作:

In my cellForRowAtIndexPath method, I do the following:

        var event: AnyObject? = eventContainerArray[indexPath.row]

        if let unwrappedEvent: AnyObject = event {
            let eventTitle = unwrappedEvent["title"] as? String
            let eventDate = unwrappedEvent["date"] as? String
            let eventDescription = unwrappedEvent["description"] as String
            let eventImage = unwrappedEvent["image"] as? UIImage

            println(eventImage)

            if (eventImage != nil){


                cell.loadItem(date: eventDate!, title: eventTitle!, description: eventDescription, image: eventImage!)}


            else {


                let testImage: UIImage = UIImage(named: "test-image.png")!

                cell.loadItem(date: eventDate!, title: eventTitle!, description: eventDescription, image: testImage )


            }

        }
    }

    return cell
}

我在我的PFQuery中使用println(),并且将其视为正在加载的对象的一部分:image = "<PFFile: 0x7fee62420b00>";

I'm using println() with my PFQuery, and I am seeing this as part of the object that's loading in: image = "<PFFile: 0x7fee62420b00>";

因此,我将标题,日期,描述等作为上述eventContainerArray的一部分很好地加载,但是当我查看eventImage时,每次都为零.在上面的代码中,它始终默认为加载test-image.png,因为该图像即将变为nil.我只是简单地以不正确的方式处理该PFFile吗?不知道为什么它不起作用.

So I'm getting title, date, description, etc. all loading fine as part of the above eventContainerArray, but when I look at eventImage, it's nil every time. In the code above, it always defaults to loading test-image.png, being that the image is coming up nil. Am I simply handling that PFFile the improper way? Not sure why it's not working.

谢谢!

推荐答案

您的image可能是PFFile对象.您需要加载它才能从中获取UIImage对象.

Your image is likely a PFFile object. You will need to load it to get an UIImage object from it.

let userImageFile = unwrappedEvent["image"] as PFFile
userImageFile.getDataInBackgroundWithBlock {
    (imageData: NSData!, error: NSError!) -> Void in
    if error == nil {
        let eventImage = UIImage(data:imageData)
        // do something with image here
    }
}

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

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