how can I add UIImages to my PFFile array so that

2019-09-12 22:16发布

retrieving images from Parse and showing them on a tableView, if I scroll the tableView down, app crashes and gives me "fatal error: Array index out of range"

numbers of rows is given by the number of messages in "messagesArray"

I think the problem is that in the query, if I don't find images in pointers, I say "append to the PFFile array this standard UIImage of mine called "logo" " so when I scroll down, count of imagesArray and messagesArray doesn't mach.

how can I add UIImages to my PFFile array so that if query for a PFFIle images fails, it can be replaced by appending a UIImage?

some told me to make a separate query for the image it in dispatch_async(dispatch_get_main_queue()) {}

but the main query is called itself that way.

I have this array:

var picturesArray : [PFFile] = []

in cellForRowAtIndexPath I have:

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

            }

        }

in my query, I can retrive the names of my senders by querying a "sender" column with pointers to _Users. I have:

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

Solution:

for now, is also required fix this bug (hoping Apple and Parse will take care of them sooner or later)

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

and

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

1条回答
劳资没心,怎么记你
2楼-- · 2019-09-12 23:17

Your app is crashing because you have more table view cells than pictures in picturesArray

Just check if indexPath.row < picturesArray.count before using picturesArray[x]

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

Edit:

If you want to append a PFFile with the logo image into picturesArray you can do the following

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

instead of

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

Explanation:

Here self.picturesArray.append(UIImage(named: "logo")) you were trying to add an UIImage object to and array that only accepts PFFile objects, as you declared here: var picturesArray : [PFFile] = []

You need to instantiate a new PFFile and add an UIImage to it, but the PFFile initialiser only accepts NSData to store.

So to transforma an UIImage into NSData you can use UIImagePNGRepresentation and then instantiate the PFFile with the image data.

查看更多
登录 后发表回答