问题显示来自url的图像数组 [英] Issue displaying array of image from url

查看:133
本文介绍了问题显示来自url的图像数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Firebase我打电话给很多网址将其转换为UIImage,但不是以正确的顺序显示。打印功能标记到当前索引:


0 2 1 3 4


JSON类似于:

  {
evento_1= HTTP://.altervista.org/evento1.jpeg;
evento_2=http://.altervista.org/evento2.jpeg;
evento_3=http://.altervista.org/evento3.jpeg;
evento_4=http://.altervista.org/evento4.jpeg;
evento_5=http://.altervista.org/evento5.jpeg;

$ / code $ / $ p

函数获取数据:

  ref.observeEventType(.Value,withBlock:{snapshot in 

let nsDictionary = snapshot.value as?NSDictionary


(key,value)in nsDictionary!{
dict [key as!String] = value
}

var index = 0


在字典中的(_,url){

self.loadImage(url as!String,i:index)
index ++

}

},withCancelBlock:{
print(error.description)
})

Image Loader func:

  func loadImage(urlString:String,i:Int) 
{
let url = NSURL(string:urlString)
let data = NSData(contentsOfURL:url!)

self.eventi [i] .image = UIImage (data:data!)
}


将您的Firebase快照键放入一个数组中,并将键值对放入字典中。

然后对键序列进行排序:

$ $ $ $ $ $ $ $ $ $ $ $ arrayOfKeys = Array(dict.keys)// swift
arrayOfKeys.sort {
返回$ 0< $ 1





$ b

然后你可以迭代数组来获取事件名称),它对应于你的字典中的对象(通过键访问它),它返回它的值。

或(我更喜欢这个)



只取每一个.value字典并把它们放到一个数组中,然后用eventName对数组进行排序(假设这是键)

$ $ $ eventArray.sort({$ 0.eventName> $ 1.eventName})


Through Firebase I'm calling many url to convert it to UIImage but aren't displayed in a corrected order. The print func stamp to the consolle the current index:

0 2 1 3 4

The JSON is like:

{
    "evento_1" = "http://.altervista.org/evento1.jpeg";
    "evento_2" = "http://.altervista.org/evento2.jpeg";
    "evento_3" = "http://.altervista.org/evento3.jpeg";
    "evento_4" = "http://.altervista.org/evento4.jpeg";
    "evento_5" = "http://.altervista.org/evento5.jpeg";
}

Function to get the data:

ref.observeEventType(.Value, withBlock: { snapshot in

            let nsDictionary = snapshot.value as? NSDictionary


            for (key, value) in nsDictionary! {
                dict[key as! String] = value
            }

            var index = 0


            for (_, url ) in dict {

                self.loadImage(url as! String, i: index)
                index++

            }

            }, withCancelBlock: { error in
                print(error.description)
        })

Image Loader func:

func loadImage(urlString:String,i:Int)
{      
    let url = NSURL(string: urlString)
    let data = NSData(contentsOfURL: url!)

    self.eventi[i].image = UIImage(data: data!)
 }

解决方案

Put your Firebase snapshot keys into an array and the key:value pairs into a dictionary. Then sort the key array:

arrayOfKeys = Array(dict.keys) //swift
arrayOfKeys.sort {
  return $0 < $1
}

then you can iterate over the array to get the event name (the key), which corresponds to the objects in your dictionary (access it by key) which returns it's value.

or (and I like this better)

Just take every .value dictionary and put each into an array then sort the array by the eventName (assuming that's the key)

eventArray.sort({ $0.eventName > $1.eventName })

这篇关于问题显示来自url的图像数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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