Skip to content

Instantly share code, notes, and snippets.

@benbahrenburg
Created February 18, 2017 22:14
Show Gist options
  • Select an option

  • Save benbahrenburg/e7567dc3ff0470e1296d1027469f0b03 to your computer and use it in GitHub Desktop.

Select an option

Save benbahrenburg/e7567dc3ff0470e1296d1027469f0b03 to your computer and use it in GitHub Desktop.
UIImage that implements NSDiscardableContent
final class discardableThumbnail: NSDiscardableContent {
var thumbnail: UIImage!
var accessCounter: Int = 0
init(image: UIImage) {
thumbnail = image
}
func beginContentAccess() -> Bool {
if !(self.thumbnail != nil) {
return false
}
self.accessCounter += 1
return true
}
func endContentAccess() {
if self.accessCounter > 0 {
self.accessCounter -= 1
}
}
func discardContentIfPossible() {
if self.accessCounter == 0 {
self.thumbnail = nil
}
}
func isContentDiscarded() -> Bool {
return self.thumbnail == nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment