从Swift中的Parse中检索图像

时间:2015-09-10 02:12:18

标签: swift parse-platform

我正在尝试从Parse中检索一个PFObject,它有一个Sticker的类名(类型),它有一个File类型为FileFile的属性,它包含一个.png图像。然后我尝试将现有的UIImageView设置为将其解析为UIImage。

这是我的代码。请滚动到右侧以查看我的代码评论:

import UIKit
import Parse

class DressingRoomViewController:   UIViewController,
                                    UICollectionViewDelegateFlowLayout,
                                    UICollectionViewDataSource {

    @IBOutlet weak var MirrorImageView: UIImageView!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var heightConstraint: NSLayoutConstraint!
    let identifier = "cellIdentifier"
    let dataSource = DataSource()
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    let cellSpacing: CGFloat = 2
    let cellsPerRow: CGFloat = 6
    let numberOfItems = 12

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.dataSource = self



        var query = PFQuery(className:"Sticker")
        query.getObjectInBackgroundWithId("WGYIYs0crU") { 
            (sticker: PFObject?, error: NSError?) -> Void in
            if error == nil && sticker != nil {
                println(sticker)
                if let stickerImage = sticker!.objectForKey("imageFile") as? NSData { // it goes from this line, straight to the bottom of the function, without going into the if statement or even the else statement.
                    let file = PFFile(name:"resume.txt", data:stickerImage)




                    file.getDataInBackgroundWithBlock {
                        (imageData: NSData?, error: NSError?) -> Void in
                        if (error == nil) {
                            if let imageData = imageData {
                                let image = UIImage(data:imageData)
                                self.MirrorImageView.image = image
                            }
                        } else {
                            println(error)
                        }
                    }
                }
            } else {
                println(error)
            }
        }
    }

为什么代码没有超出代码注释?

编辑:刚刚意识到我没有if语句的else语句。我把一个放入,然后进入else语句。如何执行进入if语句?

1 个答案:

答案 0 :(得分:1)

由于它正在跳过if阻止,sticker!.objectForKey("imageFile") as? NSData必须为nil。由于一些可能的原因,它可能是零。首先,确保您的Sticker类实际上具有“imageFile”属性。其次,确保对象"WGYIYs0crU"实际上已在该属性中保存了数据。您可以通过登录解析Web控制台轻松检查这些内容。

但是,我怀疑问题是你正在尝试向下转换为NSData。文件通常以PFFile保存在Parse上,因此请尝试转换为PFFile,并跳过创建新PFFile的行。像这样:

var query = PFQuery(className:"Sticker")

query.getObjectInBackgroundWithId("WGYIYs0crU") {
    (sticker: PFObject?, error: NSError?) -> Void in

    // Also, checking for nil isn't really necessary, since that's what if let does.
    if let stickerImage = sticker?["imageFile"] as? PFFile {
        file.getDataInBackgroundWithBlock {
            (imageData: NSData?, error: NSError?) -> Void in

            if let imageData = imageData {
                let image = UIImage(data:imageData)
                self.MirrorImageView.image = image
            }

            if let downloadError = error {
                println(downloadError.localizedDescription)
            }
        }
    }

    if let imageError = error {
        println(imageError.localizedDescription)
    }
}

希望有所帮助。