gpt4 book ai didi

ios - GetDataInBackgroundWithBlock | 获取数据imageData 返回 nil 值

转载 作者:行者123 更新时间:2023-11-30 14:06:57 25 4
gpt4 key购买 nike

所以我使用解析将一些对象保存到 localDatastore。

//Pin the objects here!!
var imageObject = PFObject(className: "img")
imageObject["theFile"] = imageFile
imageObject.pinInBackgroundWithBlock({ (success, error) -> Void in
if error == nil {
imageObject.saveEventually()
println("object pinned in background")
} else {
println(error)
}
})

然后在 viewDidLoad 中,我查询对象并将它们附加到 PFFile 的数组中

var query = PFQuery(className: "img")
query.fromLocalDatastore()
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil{
var objects : AnyObject = objects as! AnyObject
for object in objects as! [AnyObject]{
self.testImages.append(object["theFile"] as! PFFile)
}
} else {
println(error)
}
}

此数组现在包含数据。我正在尝试将图像插入到 tableView

if testImages.count >= 1{
testImages[indexPath.row].getDataInBackgroundWithBlock({ (imageData, error) -> Void in
if error == nil{
//The app crashes here ~ fatal error: unexpectedly found nil while unwrapping an Optional value
cell.theImage.image = UIImage(data: imageData!)
} else {
println(error)
}
})
}

最佳答案

因为您使用的是 TableView ,所以我认为最好的方法是从 Parse 下载所有图像并将其保存到数组中。然后在 cellForRowAtPath 方法中将该数组的每个索引分配给 cell.imageView.image

 var query = PFQuery(className:"img")
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
if error == nil
{
if let objects = objects as? [PFObject]
{
for one in objects
{
var pictureImage = one["theFile"] as! PFFile
pictureImage.getDataInBackgroundWithBlock({ (dataToget:NSData?, error:NSError?) -> Void in
if error == nil
{
if let Image = UIImage(data: dataToget)
{
// save the image to array
// reload the tableview
}
}
})

}
}
}
}

关于ios - GetDataInBackgroundWithBlock | 获取数据imageData 返回 nil 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32252745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com