1

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'.

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:

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

enter link description here

1
  • Try to add self: let pho = self.arrayOfImage... Then make an if let condition first, it is safe: if let pho = self.arrayOfImages ... { cell. .. = pho } Commented Sep 17, 2016 at 9:52

1 Answer 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.

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.

Sign up to request clarification or add additional context in comments.

13 Comments

An imageArray is not empty.
@pedrouan But it is contain 20 objects as you returning 20 in numberOfRowsInSection? check that
That's true. But it is not empty. Anyway, your answer doesn't solve theirs whole problem. Try to edit your answer with solution that he still needs.
@pedrouan My answer is correct OP app is crashing because there is no 20 objects inside array and when in cellForRowAtIndexPath it will try to access object it is crashing check properly.
"I don't know how to get the Image from it." he says first
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.