ios - Getting photos via PHFetchResult extremely slow with lots of photos -
i'm using yangmingshan photo picker, , it's need, i'm running problems when user has lots of photos.
i've been testing on phone 25,000+ photos , 250 albums, , when present view controller, app freezes 30 seconds before collectionview loads.
here's problematic bit of code, , i'm trying figure out if there's more optimal way fetch results.
- (void)fetchcollections { nsmutablearray *allablums = [nsmutablearray array]; phfetchresult *smartalbums = [phassetcollection fetchassetcollectionswithtype:phassetcollectiontypesmartalbum subtype:phassetcollectionsubtypealbumregular options:nil]; __block __weak void (^weakfetchalbums)(phfetchresult *collections); void (^fetchalbums)(phfetchresult *collections); weakfetchalbums = fetchalbums = ^void(phfetchresult *collections) { // create fecth options phfetchoptions *options = [phfetchoptions new]; options.predicate = [nspredicate predicatewithformat:@"mediatype = %d",phassetmediatypeimage]; options.sortdescriptors = @[[nssortdescriptor sortdescriptorwithkey:@"creationdate" ascending:no]]; (phcollection *collection in collections) { if ([collection iskindofclass:[phassetcollection class]]) { phassetcollection *assetcollection = (phassetcollection *)collection; phfetchresult *assetsfetchresult = [phasset fetchassetsinassetcollection:assetcollection options:options]; if (assetsfetchresult.count > 0) { [allablums addobject:@{@"collection": assetcollection , @"assets": assetsfetchresult}]; } } else if ([collection iskindofclass:[phcollectionlist class]]) { // if there more sub-folders, dig collection fetch albums phcollectionlist *collectionlist = (phcollectionlist *)collection; phfetchresult *fetchresult = [phcollectionlist fetchcollectionsincollectionlist:(phcollectionlist *)collectionlist options:nil]; weakfetchalbums(fetchresult); } } }; phfetchresult *toplevelusercollections = [phcollectionlist fetchtoplevelusercollectionswithoptions:nil]; fetchalbums(toplevelusercollections); (phassetcollection *collection in smartalbums) { phfetchoptions *options = [phfetchoptions new]; options.predicate = [nspredicate predicatewithformat:@"mediatype = %d",phassetmediatypeimage]; options.sortdescriptors = @[[nssortdescriptor sortdescriptorwithkey:@"creationdate" ascending:no]]; phfetchresult *assetsfetchresult = [phasset fetchassetsinassetcollection:collection options:options]; if (assetsfetchresult.count > 0) { // put "all photos" in first index if (collection.assetcollectionsubtype == phassetcollectionsubtypesmartalbumuserlibrary) { [allablums insertobject:@{@"collection": collection , @"assets": assetsfetchresult} atindex:0]; } else { [allablums addobject:@{@"collection": collection , @"assets": assetsfetchresult}]; } } } self.collectionitems = [allablums copy]; }
Comments
Post a Comment