将JSON图像数据存储在数组中并使用Swift在tableViewCell中显示它

时间:2022-01-24 21:22:24

I am trying to store the Images from a JSON in an array and displaying it in a TableViewCell using a TableViewController. But the array not only stores the image but also the quality/size of the Image i.e. {53,53}. I don't know how to get the Image from it. I Have a UIImage in my Table View Controller name 'gameImage'.

我试图将JSON中的图像存储在一个数组中,并使用TableViewController在TableViewCell中显示它。但是阵列不仅存储图像,还存储图像的质量/大小,即{53,53}。我不知道如何从中获取图像。我的表视图控制器名称'gameImage'中有一个UIImage。

import UIKit

class ViewController: UITableViewController {

  var NumberOfRows = 5
  var arrayOfImages = [UIImage]()
  let ImageData=NSData()
  override func viewDidLoad() {

    super.viewDidLoad()
       let url = NSURL(string:"https://itunes.apple.com/us/rss/topgrossingipadapplications/limit=25/json")

    let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
    let task  = session.dataTaskWithRequest(NSURLRequest(URL: url!)) {(data,response,error) -> Void in


            let swiftyJSON  = JSON(data: data!)

        for i in 1...6{
            var theEntryArray = swiftyJSON["feed"]["entry"][i-1]["im:image"].arrayValue
            let imageArray = theEntryArray[0]["label"].string!
            let ImageUrl = NSURL(string: imageArray)

            let ImageData = NSData(contentsOfURL: ImageUrl!)

            self.arrayOfImages.append(UIImage(data:ImageData!)!)



        }

    }

    task.resume()
    // Do any additional setup after loading the view, typically from a nib.




}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->Int {
    return 20
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell


    let pho=arrayOfImages[indexPath.row]
    cell.gameImage.image = pho

    return cell
}


}

The Output of the arrayOfImages looks like:

arrayOfImages的输出如下所示:

 [<UIImage: 0x7ff893f8c990>, {53, 53}, <UIImage: 0x7ff893f62830>, {53, 53}, <UIImage: 0x7ff893c599c0>, {53, 53}, <UIImage: 0x7ff893f95740>, {53, 53}, <UIImage: 0x7ff893c5c900>, {53, 53}, <UIImage: 0x7ff893e3fee0>, {53, 53}]   

将JSON图像数据存储在数组中并使用Swift在tableViewCell中显示它

1 个解决方案

#1


1  

It is crashing because your image array may empty or not contain 20 objects, In numberOfRowsInSection you are returning 20, to solve this issue with numberOfRowsInSection you need to return the count of your imageArray like this.

它崩溃是因为你的图像数组可能是空的或者不包含20个对象,在numberOfRowsInSection中你返回20,要用numberOfRowsInSection来解决这个问题,你需要像这样返回你的imageArray的计数。

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->Int {
   return arrayOfImages.count
}

Note: It is just suggestion instead of downloading image using NSData try to use third party lib like SDWebImage and other that will give you more benefits.

注意:这只是建议而不是使用NSData下载图像尝试使用SDWebImage等第三方库,这将为您带来更多好处。

#1


1  

It is crashing because your image array may empty or not contain 20 objects, In numberOfRowsInSection you are returning 20, to solve this issue with numberOfRowsInSection you need to return the count of your imageArray like this.

它崩溃是因为你的图像数组可能是空的或者不包含20个对象,在numberOfRowsInSection中你返回20,要用numberOfRowsInSection来解决这个问题,你需要像这样返回你的imageArray的计数。

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->Int {
   return arrayOfImages.count
}

Note: It is just suggestion instead of downloading image using NSData try to use third party lib like SDWebImage and other that will give you more benefits.

注意:这只是建议而不是使用NSData下载图像尝试使用SDWebImage等第三方库,这将为您带来更多好处。