gpt4 book ai didi

ios - 从 Parse sdk 检索 Swift 图像 - 崩溃

转载 作者:行者123 更新时间:2023-11-28 09:38:39 24 4
gpt4 key购买 nike

我正在使用 Swift 使用以下代码从 Parse 检索图像集。

var imageResources : Array<UIImage> = []
override func viewDidLoad(){
super.viewDidLoad()
self.loadImages()
}

func loadImages(){

var query = PFQuery(className: "Images")
query.orderByDescending("objectId")

query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
if(error == nil){

for object : PFObject! in objects as [PFObject] {

var thumbNail = PFFile()
thumbNail = object["image"] as PFFile
println("thumNail \(thumbNail)")
thumbNail.getDataInBackgroundWithBlock({
(imageData: NSData!, error: NSError!) in
if (error == nil) {
let image : UIImage = UIImage(data:imageData)
//image object implementation
self.imageResources.append(image)
}

})//getDataInBackgroundWithBlock - end

}//for - end

}
else{
println("Error in retrieving \(error)")
}

})//findObjectsInBackgroundWithblock - end


}

我的解析类详细信息

类(class)名称 - 图片 Parse class schema

当我运行这个函数时,它崩溃了,控制台中没有任何消息。

注意:我可以在回调中获取 PFFile 对象的集合。

我换了 "thumbNail.getDataInBackgroundWithBlock({...." block 与同步函数调用 thumbNail.getData() 类似 “var imageData=thumbNail.getData() var image = UIImage(data:imageData)"

然后错误说

Warning: A long-running operation is being executed on the main thread. Break on warnBlockingOperationOnMainThread() to debug.

所以,我恢复到 thumbNail.getDataInBackGroundWithBloack({... 但是现在,控制台中没有错误显示,就像以前一样。我的方法有什么问题请告诉我。任何帮助将不胜感激...!

最佳答案

我设法重现了错误,这似乎是 PFObject 上的某种内存泄漏/僵尸。我不确定到底是为什么,但是按照以下方式重构您的代码可以消除我的错误:

func loadImages() {

var query = PFQuery(className: "Images")
query.orderByDescending("objectId")


query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
if(error == nil){

self.getImageData(objects as [PFObject])

}
else{
println("Error in retrieving \(error)")
}

})//findObjectsInBackgroundWithblock - end


}

func getImageData(objects: [PFObject]) {
for object in objects {

let thumbNail = object["image"] as PFFile

println(thumbNail)

thumbNail.getDataInBackgroundWithBlock({
(imageData: NSData!, error: NSError!) -> Void in
if (error == nil) {
let image = UIImage(data:imageData)
//image object implementation
self.imageResources.append(image)
println(image)
}

})//getDataInBackgroundWithBlock - end

}//for - end
}

编辑:顺便说一句,这也有效:

func loadImages() {

var query = PFQuery(className: "Images")
query.orderByDescending("objectId")


query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
if(error == nil){

let imageObjects = objects as [PFObject]

for object in objects {

let thumbNail = object["image"] as PFFile

thumbNail.getDataInBackgroundWithBlock({
(imageData: NSData!, error: NSError!) -> Void in
if (error == nil) {
let image = UIImage(data:imageData)
//image object implementation
self.imageResources.append(image)
println(image)
}

})//getDataInBackgroundWithBlock - end

}//for - end

}
else{
println("Error in retrieving \(error)")
}

})//findObjectsInBackgroundWithblock - end


}

这表明错误是由于以下行造成的:

for object : PFObject! in objects as [PFObject] {

重写该行如下:

for object : PFObject in objects as [PFObject] {

同时删除错误。所以这个错误的原因似乎是你告诉程序打开一些不是可选的东西。

关于ios - 从 Parse sdk 检索 Swift 图像 - 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25764618/

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