ALAssetsLibrary est trop lent – Objective-C

Qu'est-ce qu'un moyen rapide de charger 10-20 images en plein écran à partir d'un rouleau de camera, des photos enregistrées?

J'utilise ce code, mais pour charger 10 photos, je dois attendre environ 5-10 secondes. J'utilise l'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); }]; 

La bibliothèque ALAssetsLibrary s'exécutera sur un thread distinct . Il peut donc prendre du time pour communiquer avec l' interface user et d'autres choses.

Donc utilisez -performSelectorOnMainThread:withObject:waitUntilDone: dans le bloc ALAssetsLibrary.

Changez votre code comme ci-dessous

 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 } 

Remarque : Regardez aussi ce problème .