【问题标题】:Getting error when loading image file from Parse从 Parse 加载图像文件时出错
【发布时间】:2015-06-29 22:33:21
【问题描述】:

有问题的行是“let productImageFile = productData!["productImage"] as!PFFile”,这给了我错误“致命错误:在展开可选值时意外发现 nil (lldb)”。我找到的唯一答案是确保我没有试图解开明确定义的选项(我认为这是术语),但我搞砸了选项,我在什么时候解绑,但我我没有运气。没有其他来源能够为我解决这个特定问题,我被困住了。请帮忙。

    override func viewDidLoad() {
    super.viewDidLoad()

    //Create new PFQuery to retrieve info from Parse
    var query: PFQuery = PFQuery(className: "MyProduct")

    //function to get the data
    func getProductData (){
        //call function to get the data from parse by specifyng an objectId
        query.getObjectInBackgroundWithId("XXXXXXXXXX") {
            (productData:PFObject?, error:NSError?) -> Void in
            if error == nil && productData != nil {
                //Extract values from the productData PFObject and store them in constants
                let dayOfTheWeek = productData!.objectForKey("day") as! String
                let productTitle = productData!.objectForKey("productTitle") as! String
                //-----start image loading
                let productImageFile = productData!["productImage"] as! PFFile
                productImageFile.getDataInBackgroundWithBlock {
                    (imageData: NSData?, error: NSError?) -> Void in
                    if error == nil {
                        if let imageData = imageData {
                            let image = UIImage(data:imageData)
                            self.productImageView.image = image!
                        } else {println("Could not load image.")}
                    }
                }

                //-----end image loading
                let productPrice = productData!.objectForKey("productPrice") as! String
                let productDescription = productData!.objectForKey("productDescription") as! String

                //take the saved constants and assign their values to the labels and UIImage on screen
                self.productTitleLabel.text = productTitle
                self.dayOfTheWeekLabel.text = dayOfTheWeek
                self.productPriceLabel.text = productPrice
                self.productDescriptionLabel.text = productDescription



            } else if error != nil {
                println("Could not load data from Parse")
            }



        }

    }

【问题讨论】:

    标签: ios swift parse-platform


    【解决方案1】:

    我假设并非所有产品都有图片,因此您需要更新代码如下:

    if let productImageFile = productData!["productImage"] as? PFFile {
                    productImageFile.getDataInBackgroundWithBlock {
                        (imageData: NSData?, error: NSError?) -> Void in
                        if error == nil {
                            if let imageData = imageData {
                                let image = UIImage(data:imageData)
                                self.productImageView.image = image!
                            } else {println("Could not load image.")}
                        }
                    }
    }
    

    这将保证 productImageFile 仅在 productImage 的响应为 PFFile 时才会被处理。

    【讨论】:

    • 不错!这消除了错误,但图像仍然没有出现......嗯......我想回去做更多的研究,但感谢您的回答! :)
    【解决方案2】:

    如果您的错误没有打印任何有用的信息,请确保您获得了有效数据。检查数据长度,看是否与文件大小对应。

    另外,检查 imageView 是否在主线程上设置图像,如果从后台线程调用它不会显示。

    dispatch_async(dispatch_get_main_queue(), ^ {
       self.productImageView.image = image!
    }
    

    【讨论】:

    • 我应该使用 PFImageView 而不是 UIImageView 吗?
    • 它可能会提供一些额外的功能来为您执行某些功能,例如在完成下载后自动设置图像?我看不出有任何原因常规图像视图不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多