Guest User

Untitled

a guest
Aug 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. @interface GHImageLoader : NSObject {}
  2.  
  3. + (void)loadImageAtURL:(NSURL *)imageURL completionBlock:(void (^)(NSImage *image))block;
  4.  
  5. @end
  6.  
  7.  
  8.  
  9.  
  10. #import "GHImageLoader.h"
  11.  
  12. static NSUInteger kMaxCuncurrentOperations = 4;
  13.  
  14. @interface GHImageLoader ()
  15. @property (nonatomic, retain) NSOperationQueue *operationQueue;
  16. + (GHImageLoader *)imageLoader;
  17. - (void)loadImageAtURL:(NSURL *)imageURL completionBlock:(void (^)(NSImage *image))block;
  18. @end
  19.  
  20.  
  21. @implementation GHImageLoader
  22.  
  23. @synthesize operationQueue;
  24.  
  25. - (void)dealloc {
  26.  
  27. self.operationQueue = nil;
  28. [super dealloc];
  29. }
  30.  
  31. + (GHImageLoader *)imageLoader {
  32.  
  33. static GHImageLoader *imageLoaderSingleton = nil;
  34. return imageLoaderSingleton ? : (imageLoaderSingleton = [[GHImageLoader alloc] init]);
  35. }
  36.  
  37. - (id)init {
  38.  
  39. self = [super init];
  40. if (self) {
  41. self.operationQueue = [[[NSOperationQueue alloc] init] autorelease];
  42. [self.operationQueue setMaxConcurrentOperationCount:kMaxCuncurrentOperations];
  43. }
  44.  
  45. return self;
  46. }
  47.  
  48. + (void)loadImageAtURL:(NSURL *)imageURL completionBlock:(void (^)(NSImage *image))block {
  49.  
  50. [[self imageLoader] loadImageAtURL:imageURL completionBlock:block];
  51. }
  52.  
  53. - (void)loadImageAtURL:(NSURL *)imageURL completionBlock:(void (^)(NSImage *image))block {
  54.  
  55. [self.operationQueue addOperationWithBlock:^{
  56. NSImage *imageLoaded = [[[NSImage alloc] initWithContentsOfURL:imageURL] autorelease];
  57. dispatch_async(dispatch_get_main_queue(), ^{
  58. block(imageLoaded);
  59. });
  60. }];
  61. }
  62.  
  63. @end
Add Comment
Please, Sign In to add comment