Last active
February 18, 2017 08:28
-
-
Save mpospese/676b683314bf17d15362 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
// public interface | |
@interface UBNTDiscardableThumbnail : NSObject<NSDiscardableContent> | |
+ (instancetype)discardableThumbnailWithImage:(UIImage *)image; | |
- (instancetype)initWithImage:(UIImage *)thumbnail; | |
@property (nonatomic, readonly) UIImage *thumbnail; | |
@end | |
// private interface | |
@interface UBNTDiscardableThumbnail() | |
@property (nonatomic) UIImage *thumbnail; | |
@property (nonatomic, assign) NSUInteger accessCounter; | |
@end | |
@implementation UBNTDiscardableThumbnail | |
+ (instancetype)discardableThumbnailWithImage:(UIImage *)image | |
{ | |
return [[UBNTDiscardableThumbnail alloc] initWithImage:image]; | |
} | |
- (instancetype)initWithImage:(UIImage *)image; | |
{ | |
self = [super init]; | |
if (self) | |
{ | |
_thumbnail = image; | |
} | |
return self; | |
} | |
- (BOOL)beginContentAccess | |
{ | |
if (!self.thumbnail) | |
return NO; | |
self.accessCounter++; | |
return YES; | |
} | |
- (void)endContentAccess | |
{ | |
if (self.accessCounter > 0) | |
self.accessCounter--; | |
} | |
- (void)discardContentIfPossible | |
{ | |
if (self.accessCounter == 0) | |
{ | |
self.thumbnail = nil; | |
} | |
} | |
- (BOOL)isContentDiscarded | |
{ | |
return self.thumbnail == nil; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment