SteveSmith
SteveSmith

Reputation: 269

How to remove a specific image from NSCache?

I have a collection view which has 12 images I retrieve from a network call. I use NSCache to cache them. I want to know how I can delete a specific image from there? I have provided some code below to show how I cached the images. Thanks!

  override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("imageReuseCell", forIndexPath: indexPath) as! ImageCollectionViewCell

    let image = hingeImagesArray[indexPath.row]


    //Start animating activity indicator
    cell.actitivityIndicator.startAnimating()

    if let imageURL = image.imageUrl {

        if let url = NSURL(string: imageURL) {

            //Check for cached images and if found set them to cells - works if images go off screen
            if let myImage = HomepageCollectionViewController.imageCache.objectForKey(image.imageUrl!) as? UIImage {

                cell.collectionViewImage.image = myImage


            }else {


            // Request images asynchronously so the collection view does not slow down/lag
            let task = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, response, error) -> Void in


                    // Check if there is data returned
                    guard let data = data else {

                   print("There is no data")
                        return
                    }

                    if let hingeImage = UIImage(data: data){

                      //Cache images/set key for it
                      HomepageCollectionViewController.imageCache.setObject(hingeImage, forKey: image.imageUrl!)

                       // Dispatch to the main queue
                       dispatch_async(dispatch_get_main_queue(), { () -> Void in

                        //Hide activity indicator and stop animating
                        cell.actitivityIndicator.hidden = true
                        cell.actitivityIndicator.stopAnimating()

                        //Set images to collection view
                        cell.collectionViewImage.image = hingeImage


                         })

                    }

                })

            task.resume()

           }
        }

    }

    return cell
}

Upvotes: 2

Views: 1417

Answers (1)

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52153

NSCache is the smarter version of NSDictionary class which shares the same API for retrieving, adding or removing items.

Thus, you can delete an item from it using same method as if you do from a dictionary:

HomepageCollectionViewController.imageCache.removeObjectForKey(image.imageUrl!)

You can update your code to remove the image from cache that you are just about to show:

if let myImage = HomepageCollectionViewController.imageCache.removeObjectForKey(image.imageUrl!) as? UIImage {
  // myImage was removed from cache.
  cell.collectionViewImage.image = myImage
  ...

Upvotes: 5

Related Questions