Created
August 11, 2015 01:08
-
-
Save grant-park/99ae82dca625f0d73730 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
self.imageCache.removeAllObjects() | |
} | |
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { | |
//In storyboard, my collection view cell has the identifier "PostCell" | |
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PostCell", forIndexPath: indexPath) as! CollectionViewCell | |
//I have an array called "posts" that's filled with PFObjects that have UIImage data | |
let postings: PFObject = posts[indexPath.row] | |
//setting my cell's string property called "objectId" to the PFObject's ID for the purpose of caching shown below | |
cell.objectId = postings.objectId! as String | |
let theImage: AnyObject = postings["imageFile"] as! PFFile | |
if let image = self.imageCache.objectForKey(postings.objectId!) as? UIImage { | |
cell.imageView.image = image | |
println("had this photo in cache") | |
} else { | |
theImage.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in | |
if error != nil { | |
println("error caching or downloading image") | |
return | |
} | |
let cellImage = UIImage(data:imageData!, scale: 1.0) | |
self.imageCache.setObject(cellImage!, forKey: postings.objectId!) | |
cell.imageView.image = cellImage | |
println("just added another photo to cache") | |
} | |
} | |
return cell | |
} | |
_______________________________________________________ | |
class CollectionViewCell: UICollectionViewCell { | |
var objectId: String! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment