4

I'm currenly working on a tiny project which amis to loading ALL gallery photos into my app to show some fancy effect. Unfortunately, these default thumbnail provided by system cannot meet my requirement. So I try to create my own thumbnails using "fullScreenImage". To speed up the process, I load fullScreenImage using background operations. The main methods are:

- (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 "Requesting..." log is printed in console, the "getFullScreenImage" method is locked, no any output.

I tried below methods to work around this issue:

  1. Not sharing assetsLibraryInstance (Didn't work)
  2. Don't dispatch_async when enumeate urls in "processURLs". (Did work, but I don't want to use a signle thread to process all URLs)
  3. Not using global queue, using main queue (Did work, but all these "fullScreenImage" works were doing on UI thread, making UI non-responsive)
  4. Using a private queue created with "dispatch_queue_create". (Didn't work)

So, is ALAssetsLibrary thread safe? I guess it's not... Or, is there any better way I can use to:

  1. Load fullScreenImage in background
  2. Multiple threading

Thanks!

xiaowl
  • 5,177
  • 3
  • 27
  • 28
  • Did you ever get an answer? I just encounter the same issue and would like to see if you found any solutions? – Luis Artola Dec 22 '13 at 02:48
  • @LuisArtola No, I didn't got any response from SO. But I finally solve d it myself. The idea is making `assetForURL` and `defaultRepresentation.fullScreenImage` a "atomic" operation, to make sure there is no other `assetForURL` call happened before `fullScreenImage` returns. And actually, there is some internal lock under assetslibrary, thus multiple threading doesn't benefit much... – xiaowl Dec 23 '13 at 02:01
  • I've had the same problems with ALAssetsLibrary. But with iOS8 theres a new Photos Framework that supports multi-threading and a bunch of other new features. Maybe its worth looking into if you're targeting iOS8 – Salman Hasrat Khan Dec 02 '14 at 07:27

0 Answers0