I am working on a tiny project that allows ALL galleries to be loaded into my application to show some kind of bizarre effect. Unfortunately, these default thumbnails provided by the system cannot meet my requirements. Therefore, I am trying to create my own thumbnails using "fullScreenImage". To speed up the process, I load fullScreenImage using background operations. The main methods:
- (void)getFullScreenImage:(NSURL *)url success:(void(^)(UIImage *))callback { NSLog(@"Requesting %@", url); [assetsLibraryInstance assetForURL:url resultBlock:^(ALAsset *asset) { callback(asset.defaultRepresentation.fullScreenImage); } failureBlock:nil]; } - (void)processURLs:(NSArray *)urls { for (NSURL *url in urls) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) ^{ [self getFullScreenImage:url success:^(UIImage *img) { NSLog(@"Got image %@", img); }] ; }); } }
Only the "Request ..." log is printed in the console, the "getFullScreenImage" method is blocked, there will be no exit.
I tried using methods to solve this problem:
- Do not use LibraryInstance resources (not working)
- Do not send async when listing URLs in "processURLs". (It worked, but I do not want to use the signature stream to process all URLs)
- Do not use the global queue using the main queue (it worked, but all these "fullScreenImage" worked in the user interface thread, which makes the interface inactive)
- Using a private queue created using dispatch_queue_create. (Does not work)
So is the ALAssetsLibrary thread safe? I think this is not the case ... Or, is there a better way I can use for:
- Download fullScreenImage in the background
- Multithreading
Thanks!
source share