我怎样才能将UIImage添加到我的PFFile数组,以便如果查询PFFIle图像失败,它可以通过附加一个UIImage?

从Parse中检索图像,并在tableView上显示它们,如果我向下滚动tableView,应用程序崩溃,并给我“致命错误:数组索引超出范围”

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

我认为问题是,在查询中,如果我没有find图像的指针,我说:“附加到PFFile数组这个标准的UIImage我的称为”标志“”所以当我向下滚动,计数imagesArray和messagesArray doesn马克。

我怎样才能将UIImage添加到我的PFFile数组,以便如果查询PFFIle图像失败,它可以被replace为附加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的“发件人”列来_Users发件人的_Users 。 我有:

 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")) } 

解:

现在也需要修复这个 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!") } 

你的应用程序崩溃,因为你有更多的表格视图单元比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") } 

编辑:

如果您想将徽标图像的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