CODEX – 4 STORE UIIMAGE INTO NSCACHE

NSCache UIImage

  • Declare var Global
    • var cache:NSCache!
  • Declare Assign NSCache() inside viewDidLoad()
    • self.cache = NSCache()

Now , Write as

if (self.cache.objectForKey(indexPath.row) != nil)
{
                       cell.imageView?.image = self.cache.objectForKey(indexPath.row) as? UIImage
 }
else
{
                            let img:UIImage! = UIImage(data: data)
                            updateCell.imageView?.image = img
                self.cache.setObject(img, forKey: indexPath.row)
   }
            
  • By applying this we can get images fast and No need to loading images every time, Because it’s  store in to the cache .
  • So UIImage loads first time and after every time it will loading cache file of that view.

Leave a Reply

Your email address will not be published. Required fields are marked *