如何添加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?

查看:135
本文介绍了如何添加UIImages我PFFile阵列,因此,如果查询的PFFIle图像失败时,它可以通过追加一个UIImage的更换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从解析图像检索和显示他们在的tableView,如果我滚动的tableView下,应用程序崩溃,给我致命错误:数组索引超出范围

行号是由消息的数量在messagesArray

给出

我认为问题是,在查询时,如果我没有发现指针的图片,我说追加到 PFFile 数组这个标准的UIImage 所以当我向下滚动,算上imagesArray和messagesArray不马赫。

我怎么能添加UIImages我PFFile阵列,因此,如果查询的PFFIle图像失败时,它可以通过追加一个UIImage取代?

有的告诉我,使该图像是一个单独的查询中 dispatch_async(dispatch_get_main_queue()){}

但主要的查询称为自己的方式。

我有这样的数组:

  VAR picturesArray:[PFFile] = []

的cellForRowAtIndexPath 我有:

  self.picturesArray [indexPath.row] .getDataInBackgroundWithBlock {(?为imageData:NSData的?错误:NSError) -  GT;在无效            如果错误== {为零
                让图像= UIImage的(数据:为imageData!)
                cell.senderProfileImage?=图像配图片            }其他{                打印(B计划)
                ?cell.senderProfileImage =图像配的UIImage(命名为:标识)            }        }

在我的查询中,我可以通过查询的指针是发件人一栏 _Users retrive我的发件人的姓名。我有:

 如果让theName = singleObject.objectForKey(发件人)?objectForKey(FIRST_NAME)作为?字符串{
                        //这个检索指针发件人名称中的消息
                        self.sendersArray.append(theName)//填充名称数组
                    }其他{
                        如果让messageSender,和= singleObject [senderNickname]作为?字符串{
                            self.sendersArray.append(messageSender,和)
                        }
                    }                    如果让profilePicture = singleObject.objectForKey(发件人)?objectForKey(profile_picture)作为? PFFile {                    self.picturesArray.append(profilePicture)                    }其他{
                        //我觉得这就是问题所在:
                        打印(无指针中发现用户图像)
                        self.picturesArray.append(UIImage的(命名为:标识))
                    }

解决方案:

目前来说,还需要修正<一个href=\"http://stackoverflow.com/questions/32761042/swift-app-transport-security-has-blocked-a-cleartext-http-resource\">this错误(希望苹果和解析会照顾他们迟早)

  //很新,它的确定
                    如果让theName = singleObject.objectForKey(发件人)?objectForKey(FIRST_NAME)作为?字符串{
                        //这个检索指针发件人名称中的消息
                        self.sendersArray.append(theName)//填充名称数组
                    }其他{
                        如果让messageSender,和= singleObject [senderNickname]作为?字符串{
                            self.sendersArray.append(messageSender,和)
                        }
                    }                    //很新:此修复致命错误:数组索引超出范围
                    如果让profilePicture = singleObject.objectForKey(发件人)?objectForKey(profile_picture)作为? PFFile {                    self.picturesArray.append(profilePicture)                    }其他{
                        //我觉得这就是问题所在:
                        打印(无指针中发现用户图像)
// self.picturesArray.append(的UIImage(命名为:标识))
                        让我们为imageData:NSData的= UIImagePNGRe presentation(的UIImage(命名为:标识)!)!
                        self.picturesArray.append(PFFile(数据:为imageData))
                    }

  //此修复与不同的行数和图像坠毁
        如果indexPath.row&LT; picturesArray.count {
            self.picturesArray [indexPath.row] .getDataInBackgroundWithBlock {(为imageData:NSData的?错误:NSError?) - GT;在无效                如果错误== {为零
                    让图像= UIImage的(数据:为imageData!)
                    cell.senderProfileImage?=图像配图片
                }            }
        }其他{
?// cell.senderProfileImage =图像配的UIImage(命名为:标识)
            打印(无照片!)
        }


解决方案

您的应用程序崩溃,因为你有更多的表视图细胞比在picturesArray图片

只是检查是否indexPath.row&LT;使用前picturesArray.count picturesArray [X]

 如果indexPath.row&LT; picturesArray.count {
    self.picturesArray [indexPath.row] .getDataInBackgroundWithBlock {(为imageData:NSData的?错误:NSError?) - GT;在无效        如果错误== {为零
            让图像= UIImage的(数据:为imageData!)
            cell.senderProfileImage?=图像配图片
        }    }
}其他{
    ?cell.senderProfileImage =图像配的UIImage(命名为:标识)
}

编辑:

如果你想要一个PFFile追加的标志图像插入picturesArray你可以做以下

 让我们为imageData:NSData的= UIImagePNGRe presentation(的UIImage(命名为:imagename)!)!
self.picturesArray.append(PFFile(数据:为imageData))

而不是

  self.picturesArray.append(的UIImage(命名为:标识))

说明:

下面 self.picturesArray.append(的UIImage(命名为:标识))你想添加一个的UIImage 对象和数组,只接受 PFFile 的对象,当你宣称这里: VAR picturesArray:[PFFile] = []

您需要实例化一个新的 PFFile 并添加的UIImage 来它,但 PFFile 初始化器只接受的NSData 储存。

所以要transforma一个的UIImage 的NSData 您可以使用 UIImagePNGRe presentation ,然后实例化 PFFile 与图像数据。

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

解决方案

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.

这篇关于如何添加UIImages我PFFile阵列,因此,如果查询的PFFIle图像失败时,它可以通过追加一个UIImage的更换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆