简体   繁体   English

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

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

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" 从Parse检索图像并将其显示在tableView上,如果我向下滚动tableView,应用程序将崩溃,并显示“严重错误:数组索引超出范围”

numbers of rows is given by the number of messages in "messagesArray" 行数由“ 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. 我认为问题是,在查询中,如果我找不到指针中的图像,我会说“将这个标准的UIImage追加到PFFile数组中,称为“徽标””,因此当我向下滚动时,imagesArray和messagesArray的计数不会不是马赫。

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? 如何将UIImages添加到PFFile数组中,以便在查询PFFIle图像失败时可以通过附加UIImage来替换它?

some told me to make a separate query for the image it in dispatch_async(dispatch_get_main_queue()) {} 有人告诉我在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: 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")

            }

        }

in my query, I can retrive the names of my senders by querying a "sender" column with pointers to _Users . 在查询中,我可以通过使用指向_Users指针查询“发件人”列来_Users发件人的_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) 目前,还需要修复错误(希望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))
                    }

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

Your app is crashing because you have more table view cells than pictures in picturesArray 您的应用程序崩溃是因为您的表视图单元格比pictureArray中的图片多

Just check if indexPath.row < picturesArray.count before using picturesArray[x] 只需在使用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")
}

Edit: 编辑:

If you want to append a PFFile with the logo image into picturesArray you can do the following 如果要将带有徽标图像的PFFile附加到picturesArray中,可以执行以下操作

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] = [] 在这里, self.picturesArray.append(UIImage(named: "logo"))试图将UIImage对象添加到仅接受PFFile对象的array中,如您在此处声明的那样: 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. 您需要实例化一个新的PFFile并向其中添加一个UIImage ,但是PFFile初始化程序仅接受NSData进行存储。

So to transforma an UIImage into NSData you can use UIImagePNGRepresentation and then instantiate the PFFile with the image data. 因此,要将UIImage转换为NSData ,可以使用UIImagePNGRepresentation ,然后使用图像数据实例化PFFile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM