2

What is a fast way to load 10-20 fullscreen images from a camera roll, saved photos?

I'm using this code, but to load 10 photos I need to wait about 5-10 seconds. I'm using iPhone 4S.

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    if(_savedPhotos.count>=11) *stop = YES;
    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *needToStop) {
        NSLog(@"%d",index);
        if(_savedPhotos.count<11)
        {
            UIImage *image = [UIImage imageWithCGImage:result.defaultRepresentation.fullScreenImage];
            [_savedPhotos addObject:image];
        }
        else
        {
            *needToStop = YES;
        }
    }];
} failureBlock:^(NSError *error) {
    NSLog(@"%@",error.description);
}];
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Timur Mustafaev
  • 4,869
  • 9
  • 63
  • 109

1 Answers1

4

The ALAssetsLibrary library will run on a separate thread. So it may take time to communicate with the UI related and other stuff.

So use -performSelectorOnMainThread:withObject:waitUntilDone: inside the ALAssetsLibrary block.

Change your code as below

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *needToStop) {
            NSLog(@"%d",index);
            UIImage *image = [UIImage imageWithCGImage:result.defaultRepresentation.fullScreenImage];
            [self performSelectorOnMainThread:@selector(usePhotolibraryimage:) withObject:image waitUntilDone:NO];
        }];
    }

    failureBlock:^(NSError *error) {
           NSLog(@"%@",error.description);
    }];

- (void)usePhotolibraryimage:(UiImage *)myImage{

    //Do your all UI related and all stuff here
}

Note:Look on this issue too.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • I've implemented your code and changed fullscreen image to thumbnail, and save ALAsset *result to NSMutableArray, and access it when I need. It is much faster and takes less memory. – Timur Mustafaev Oct 22 '12 at 11:17
  • you don't want to convert the image to thumbnail manually.. ALAssetsLibrary will directly provide thumbnails – Shamsudheen TK Oct 22 '12 at 11:20
  • LAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) { CGImageRef iref = [myasset thumbnail]; if (iref) { importMediaSaveImage.image = [UIImage imageWithCGImage:iref]; } – Shamsudheen TK Oct 22 '12 at 11:21
  • also please see my question here http://stackoverflow.com/questions/11580918/nsblockoperation-or-nsoperation-with-alasset-block-to-display-photo-library-imag – Shamsudheen TK Oct 22 '12 at 11:23
  • One thing to be aware of: if your user has not given Photos permissions yet, they will be asked the first time via a UIAlertView. This must be on the main thread. So take that into consideration. – Eric G Mar 05 '14 at 18:46