Download image from URL in a loop to view the collection

I am trying to create an image collection view. I tried these solutions to upload images:
one
two
three
And
four

var tempObject = json["photos"]
                for var i = 0; i < tempObject.count; i++
                {
                    var tempImage = UIImage()

                    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

                        tempImage =  UIImage(data: NSData(contentsOfURL: NSURL(string:"https://developer.apple.com/swift/images/swift-og.png")!)!)!
                    })

                    swiftImages.insert(tempImage, atIndex: i)
                }

                dispatch_async(dispatch_get_main_queue()) {

                    self.collectionView?.reloadData()
                }

But I can not get it to display images. I am new to networking with fast.

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! ImageCollectionViewCell
        cell.imageView.image = swiftImages[indexPath.row]
        return cell
    }
+4
source share
2 answers

UIImage() (, dispatch_async) swiftImages. , dispatch_async , var tempImage = UIImage(). , , . :

            var tempObject = json["photos"]
            for var i = 0; i < tempObject.count; i++
            {
                dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

                    var tempImage =  UIImage(data: NSData(contentsOfURL: NSURL(string:"https://developer.apple.com/swift/images/swift-og.png")!)!)!
                    swiftImages.insert(tempImage, atIndex: i)
                    dispatch_async(dispatch_get_main_queue()) {

                       self.collectionView?.reloadData()
                    }
                })
            }

btw - , - AlamofireImage.

+1

:

                var tempImage = UIImage()

                dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

                    tempImage =  UIImage(data: NSData(contentsOfURL: NSURL(string:"https://developer.apple.com/swift/images/swift-og.png")!)!)!
                })

                swiftImages.insert(tempImage, atIndex: i)

tempImage , , swiftImages .

, ​​ SDWebImage. . , :

import SDWebImage

let imageView = UIImageView()
imageView.sd_setImageWithURL( ... url ... )
0

Source: https://habr.com/ru/post/1625975/


All Articles