0

I am trying to display a list of items. My end game is displaying a grid of images from an array of Strings that are URLs and file names on my IOS app.

I have a CollectionView that is displaying a grid of images via an array of String filenames, and that works:

Array:

var tableImages: [String] = ["car.jpg", "bus.jpg", "plane.jpg"]

code that works:

cell.imgCell.image = UIImage(named: tableImages[indexPath.row])

My next step is figuring out the code for displaying a grid of images via an array of String urls. And that is my question. How do I display an image from a url?

Array:

var tableImages: [String] = ["http://www.hotelstmarie.com/wp-content/uploads/2014/12/HSM_Guest-Rooms_1500x750-06-02-2015.jpg", "http://media-cdn.tripadvisor.com/media/photo-s/03/4f/b0/58/aviva-by-kameel.jpg"]

code that works:

?

Notes:

I have checked other stackoverflow questions on the matter but they are from years ago and the move to swift has made their answers out of date.

After I figure out how to display url images I will just make some if statements to recognize url or filename to make it to my endgame, but that is not part of this question.

EDIT 1

Code around my code above:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: CollViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollViewCell
    cell.labelCell.text = tableData[indexPath.row]
    cell.imgCell.image = UIImage(named: tableImages[indexPath.row])
    return cell
}

EDIT 2

Using Alamofire:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: CollViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollViewCell
    cell.labelCell.text = tableData[indexPath.row]
    let imageUrl = NSURL(string: tableImages[indexPath.row])
    cell.imgCell.af_setImageWithUrl(imageUrl, placeholderImage: nil, filter: nil, imageTransition: .CrossDissolve(0.2))
    return cell
}

Gives this error:

Value of type 'UIImageView' has no member 'af_setImageWithUrl'

4 Answers 4

2
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: CollViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollViewCell
    cell.labelCell.text = tableData[indexPath.row]

    if let url = NSURL(string: tableImages[indexPath.row])
    {
        if let data = NSData(contentsOfURL: url)
        {
            cell.imgCell.image = UIImage(data: data)
        }
    }

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

Comments

1

The way you are setting your image here:

cell.imgCell.image = UIImage(named: tableImages[indexPath.row])

Only works for local images in your project, so your all will try to look for an image named the same as your url locally.

While you could load the image manually, the best alternative is to use an image loading library to deal with common issues, like avoiding old images to load on top of your new ones when your view gets recycled.

In swift i have used AlamofireImage like so:

let imageUrl = NSURL(string: tableImages[indexPath.row])
cell.imgCell.af_setImageWithURL(imageUrl, placeholderImage: nil, filter: nil, imageTransition: .CrossDissolve(0.2))

as you can see, this library uses swift's extensions which greatly simplifies it's api.

3 Comments

Thanks for the suggestion, I just tried it, can you see my edit 2?
@Rorschach At the top of your file you need to add import AlamofireImage to your project. Here's a link to the repo with install instructions: github.com/Alamofire/AlamofireImage (it uses either Cocoapods or Carthage)
@Rorschach my bad, i misspelled the method name (i typed af_setImageWithUrl and it was af_setImageWithURL). I Edited my answer with the correct method.
1

You would write:

if let url = NSURL(string: tableImages[indexPath.row]){
    let data = NSData(contentsOfURL: url)
    let image = UIImage(data: data!)
    cell.imgCell.image = image
}

Although I recommend using an existing Swift library (like Kingfisher) that handles thread management and caching for you, so your app doesn't run out of memory or freeze while it's downloading the images. I currently use SDWebImage.

2 Comments

This goes through with no errors, but no images are being displayed, Ill work with my code and see whats up and accept this answer if it ends up leading me to the answer
@Rorschach The problem could be that the images are just taking a while to download. If it is, you're better off using one of the libraries I mentioned, or others (like Alamofire) :)
0

I have used Swift version of UIImageView+AFNetworking from AFNetworking library which you can get from github. Very easy to use. Just drop the file in your project and you can start using it as following:

   cell.imageView.setImageWithUrl(imageUrl!,placeHolderImage: UIImage(named: "placeholder"))

Comments

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.