gpt4 book ai didi

ios - 如何将 UIImages 添加到我的 PFFile 数组中,以便在查询 PFFIle 图像失败时,可以通过附加 UIImage 来替换它?

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

从 Parse 检索图像并将其显示在 tableView 上,如果我向下滚动 tableView,应用程序崩溃并给出“ fatal error :数组索引超出范围”

行数由“messagesArray”中的消息数给出

我认为问题在于,在查询中,如果我在指针中找不到图像,我会说“将我的标准 UIImage 附加到 PFFile 数组称为“logo”“所以当我向下滚动时,imagesArray 和 messagesArray 的计数不匹配。

如何将 UIImages 添加到我的 PFFile 数组中,以便在查询 PFFIle 图像失败时,可以通过附加 UIImage 来替换它?

有人告诉我在 dispatch_async(dispatch_get_main_queue()) {}

中对图像进行单独查询

但主查询本身就是这样调用的。

我有这个数组:

var picturesArray : [PFFile] = []

cellForRowAtIndexPath中我有:

self.picturesArray[indexPath.row].getDataInBackgroundWithBlock { (imageData: NSData?, error:NSError?) -> Void in

if error == nil {
let image = UIImage(data: imageData!)
cell.senderProfileImage?.image = image

} else {

print("plan B")
cell.senderProfileImage?.image = UIImage(named: "logo")

}

}

在我的查询中,我可以通过查询带有指向 _Users 指针的“sender”列来检索发件人的姓名。我有:

if let theName = singleObject.objectForKey("sender")?.objectForKey("first_name") as? String {
// this retrieves names from pointer "sender" in "Messages"
self.sendersArray.append(theName) //populate the array with names
} else {
if let messageSender = singleObject["senderNickname"] as? String {
self.sendersArray.append(messageSender)
}
}

if let profilePicture = singleObject.objectForKey("sender")?.objectForKey("profile_picture") as? PFFile {

self.picturesArray.append(profilePicture)

} else {
//I think this is the problem:
print("no image found in pointer to users")
self.picturesArray.append(UIImage(named: "logo"))
}

解决方案:

目前还需要修复 this bug(希望Apple和Parse迟早能够解决它们)

//very new, it's ok
if let theName = singleObject.objectForKey("sender")?.objectForKey("first_name") as? String {
// this retrieves names from pointer "sender" in "Messages"
self.sendersArray.append(theName) //populate the array with names
} else {
if let messageSender = singleObject["senderNickname"] as? String {
self.sendersArray.append(messageSender)
}
}



//very new : this fix fatal error: Array index out of range
if let profilePicture = singleObject.objectForKey("sender")?.objectForKey("profile_picture") as? PFFile {

self.picturesArray.append(profilePicture)

} else {
//I think this is the problem:
print("no image found in pointer to users")
// self.picturesArray.append(UIImage(named: "logo"))
let imageData:NSData = UIImagePNGRepresentation(UIImage(named: "logo")!)!
self.picturesArray.append(PFFile(data: imageData))
}

 //this fixes the crash related to different number of rows and images
if indexPath.row < picturesArray.count {
self.picturesArray[indexPath.row].getDataInBackgroundWithBlock { (imageData: NSData?, error:NSError?) -> Void in

if error == nil {
let image = UIImage(data: imageData!)
cell.senderProfileImage?.image = image
}

}
} else {
// cell.senderProfileImage?.image = UIImage(named: "logo")
print("no foto!")
}

最佳答案

您的应用程序正在崩溃,因为您的表格 View 单元格多于 picturesArray 中的图片

在使用 picturesArray[x] 之前只需检查 indexPath.row < picturesArray.count

if indexPath.row < picturesArray.count {
self.picturesArray[indexPath.row].getDataInBackgroundWithBlock { (imageData: NSData?, error:NSError?) -> Void in

if error == nil {
let image = UIImage(data: imageData!)
cell.senderProfileImage?.image = image
}

}
} else {
cell.senderProfileImage?.image = UIImage(named: "logo")
}

编辑:

如果您想将带有 Logo 图像的 PFFile 附加到 picturesArray 中,您可以执行以下操作

let imageData:NSData = UIImagePNGRepresentation(UIImage(named: "imagename")!)!
self.picturesArray.append(PFFile(data: imageData))

而不是

self.picturesArray.append(UIImage(named: "logo"))

说明:

这里 self.picturesArray.append(UIImage(named: "logo")) 您试图将一个 UIImage 对象添加到仅接受 的数组PFFile 对象,如您在此处声明的那样:var picturesArray : [PFFile] = []

您需要实例化一个新的 PFFile 并向其添加 UIImage,但 PFFile 初始化程序仅接受 NSData 存储。

因此,要将 UIImage 转换为 NSData,您可以使用 UIImagePNGRepresentation,然后用图像实例化 PFFile数据。

关于ios - 如何将 UIImages 添加到我的 PFFile 数组中,以便在查询 PFFIle 图像失败时,可以通过附加 UIImage 来替换它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33755310/

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