views:

114

answers:

1

So this one is pretty odd ad I'm not sure if the trouble is with the AssetsLibrary API, but I can't figure out what else might be happening.

I am loading an array with ALAssets using the -enumerateAssetsUsingBlock method on ALAssetsGroup. When it completes, I am loading a custom image scroller. As the scroller finishes scrolling, I use NSInvocationOperations to load the images for the currently visible views (pages) from the photo library on disk. Once the image is loaded and is cached, it notifies the delegate which then grabs the image from the cache and displays it in an image view in the scroller.

Everything works fine, but the time it takes from when -setImage: actually gets called to the time it actually shows up visibly on the screen is unbearable--sometimes 10 seconds or more to actually show up.

I have tried it both with and without image resizing which adds almost nothing to the processing time when I do the resizing. As I said, the slowdown is somewhere after I call -setImage on the image view. Is anyone aware of some sort of aspect of the AssetLibrary API that might cause this?

Here's some relevant code:

- (void)setImagesForVisiblePages;
{
  for (MomentImageView *page in visiblePages)
  {
    int index = [page index];
    ALAsset *asset = [photos objectAtIndex:index];

    UIImage *image = [assetImagesDictionary objectForKey:[self idForAsset:asset]];

    // If the image has already been cached, load it into the
    // image view. Otherwise, request the image be loaded from disk.
    if (image)
    {
      [[page imageView] setImage:image];
    }
    else {
      [self requestLoadImageForAsset:asset];      
      [[page imageView] setImage:nil];
    }
  }  
}
A: 

This will probably mess up any web searches looking to solve problems with the AssetsLibrary, so for that I apologize. It turns out that the problem wasn't the AssetsLibrary at all, but rather my use of multi-threading. Once the image finished loading, I was posting a notification using the default NSNotificationCenter. It was posting it on the background thread which was then updating (or trying to update, at least) the UIImageView with -setImage. Once I changed it to use -performSelectorOnMainThread and had that selector set the image instead, all was well.

Seems no matter how familiar I get with multi-threading, I still forget the little gotchas from time to time.

Matt Long