daltoniam / Skeets

Fetch, cache, and display images via HTTP in Swift.
Apache License 2.0
191 stars 21 forks source link

Image.fetch not downloading correct image #9

Closed Joropo closed 9 years ago

Joropo commented 9 years ago

the cells in my collection view will all sometimes display the same image, despite having a different URL. Or occasionally, odd cells will host the wrong picture. Any idea why this is?

Joropo commented 9 years ago

i'm assuming it's a collision problem?

daltoniam commented 9 years ago

It might be an issue with recycling logic. Check this out: http://vluxe.io/build-swift-people.html

Joropo commented 9 years ago

unfortunately, I tried to implement that and had no success :(

daltoniam commented 9 years ago

That truly is :confused:. I am not sure what you are seeing without knowing the code implementation. I think the SwiftPeople project works as expected and doesn't exhibit the recycling logic issue with the latest 0.9.3 tag. Do you have a code example of this issue?

Joropo commented 9 years ago
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    var feedPost = collectionView.dequeueReusableCellWithReuseIdentifier(feedPostsReuseId, forIndexPath: indexPath) as feedPosts
    feedPost.backgroundColor = UIColor.whiteColor()

    var imagePost = collectionView.dequeueReusableCellWithReuseIdentifier(imagePostCellReuseId, forIndexPath: indexPath) as imagePostCell
    imagePost.backgroundColor = UIColor.whiteColor()

    var status = collectionView.dequeueReusableCellWithReuseIdentifier(statusCellReuseId, forIndexPath: indexPath) as statusCell
    status.backgroundColor = UIColor.whiteColor()

    var errorHappened = collectionView.dequeueReusableCellWithReuseIdentifier(errorCellReuseId, forIndexPath: indexPath) as errorCell
    errorHappened.backgroundColor = UIColor.whiteColor()
        //array full of dictionaries
        var feedPostArray = self.feedArray
        //if it's not empty, fill the cells
        if feedPostArray.count > 0 {
             //grab a user
            if let feedPostPerson = self.feedArray[indexPath.row] as? NSDictionary {
                var avatar = ""
                var smallImage = ""
                 var text = ""
                if let author = feedPostPerson["author"] as? NSDictionary {
                    if let aAvatar = author["avatar"] as? String {
                        avatar = aAvatar
                    }
                //are there images in this feed post?
                if let authorImages = feedPostPerson["image"] as? NSDictionary {

                    if let aSmallImage = authorImages["144"] as? String {
                        smallImage = aSmallImage
                    }
                    //images with no status attached to it
                    if text == "" {
                        //get profile picture
                        ImageManager.fetch(avatar,
                            progress: { (status: Double) in
                                //println("getting avatar...")
                            },success: { (profPic: NSData) in
                                //println("got the prof image!")
                                imagePost.profilePicture.image = UIImage(data: profPic) //set the image data
                            }, failure: { (error: NSError) in
                                println("failed to get an image: \(error)")
                        })

                        imagePost.postImage.image = nil
                        if imagePost.currentImgURL != "" {
                            ImageManager.cancel(imagePost.currentImgURL)
                        }

                        //get post
                        ImageManager.fetch(smallImage,
                            progress: { (status: Double) in
                                //println("getting avatar...")
                            },success: { (profPic: NSData) in
                                //println("got the small image!")
                                imagePost.postImage.image = UIImage(data: profPic) //set the image data
                            }, failure: { (error: NSError) in
                                println("failed to get an image: \(error)")
                        })

                        return imagePost
                    }

                    //image and text
                    ImageManager.fetch(avatar,
                        progress: { (status: Double) in
                            //println("getting avatar..")
                        },success: { (profPic: NSData) in
                            //println("got the prof!")
                            feedPost.profilePicture.image = UIImage(data: profPic) //set the image data
                        }, failure: { (error: NSError) in
                            println("failed to get an image: \(error)")
                    })

                    feedPost.postImage.image = nil
                    if feedPost.currentImgURL != "" {
                        ImageManager.cancel(feedPost.currentImgURL)
                    }
                    //get post
                    ImageManager.fetch(smallImage,
                        progress: { (status: Double) in
                            //println("getting avatar")
                        },success: { (profPic: NSData) in
                            //println("got the small image!")
                            feedPost.postImage.image = UIImage(data: profPic) //set the image data
                        }, failure: { (error: NSError) in
                            println("failed to get an image: \(error)")
                    })

                    feedPost.statusPost.text = text
                    return feedPost
                }
                //end of post with images

                //this is where you have posts with 
                //text only
                ImageManager.fetch(avatar,
                    progress: { (status: Double) in
                    },success: { (profPic: NSData) in
                        status.profilePicture.image = UIImage(data: profPic) //set the image data
                    }, failure: { (error: NSError) in
                        println("failed to get an image: \(error)")
                })
                status.statusPost.text = text
                return status
            }

        }
        return errorHappened

    //end of feed
    }
daltoniam commented 9 years ago

That is a fair amount code :smile:. I don't see anywhere you assigned currentImgURL to store the currently loading image. Also I would try and cancel the image url before fetching the new one.