Created
August 31, 2019 07:39
-
-
Save laevandus/6bcf8bf219e6a4d7698da31e0aa04109 to your computer and use it in GitHub Desktop.
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
#import "ImageFrameScheduler.h" | |
#import "ImageIO/CGImageAnimation.h" // Xcode 11 beta 7 - CGImageAnimation.h is not in umbrella header IOImage.h | |
@interface ImageFrameScheduler() | |
@property (readwrite) NSURL *imageURL; | |
@property (getter=isStopping) BOOL stopping; | |
@end | |
@implementation ImageFrameScheduler | |
- (instancetype)initWithURL:(NSURL *)imageURL { | |
if (self = [super init]) { | |
self.imageURL = imageURL; | |
} | |
return self; | |
} | |
- (BOOL)startWithFrameHandler:(void (^)(NSInteger, CGImageRef))handler { | |
__weak ImageFrameScheduler *weakSelf = self; | |
OSStatus status = CGAnimateImageAtURLWithBlock((CFURLRef)self.imageURL, nil, ^(size_t index, CGImageRef _Nonnull image, bool* _Nonnull stop) { | |
handler(index, image); | |
*stop = weakSelf.isStopping; | |
}); | |
// See CGImageAnimationStatus for errors | |
return status == noErr; | |
} | |
- (void)stop { | |
self.stopping = YES; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment