Forked from benbahrenburg/discardableThumbnail.swift
Created
November 14, 2017 00:27
-
-
Save chriswill0w/8bae5e2f62da78e7087a5fc3f1607890 to your computer and use it in GitHub Desktop.
UIImage that implements NSDiscardableContent
This file contains 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
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