ios - How to use PHCachingImageManager -


i know there question on so, don't think given answer satisfying/complete: how can ios photos app can show hundreds of photos in 1 screen?

what want achieve

i want image selection in whatsapp (ios) (see screenshot). when open camera, there horizontal slider can see images gallery.

whatsapp screenshot

what tried

right have following code in appdelegate:

let options = phfetchoptions()  options.sortdescriptors = [     nssortdescriptor(key: "creationdate", ascending: true) ] if let results = phasset.fetchassetswithmediatype(.image, options: options) {      results.enumerateobjectsusingblock { (object, idx, _) in         if let asset = object as? phasset {             variables.assets.append(asset)         }     }      println(variables.assets.count)      variables.imagemanager.startcachingimagesforassets(variables.assets, targetsize: cgsizemake(viewwidth, viewwidth), contentmode: .aspectfill, options: self.requestoptions) } 

later load images in uitableviewcontroller , call following function on scroll:

func fetchphotoatindex(index : int) {      let asset = variables.assets[variables.assets.count - 1 - index] phasset      variables.imagemanager.requestimageforasset(asset, targetsize: cgsizemake(self.viewwidth, self.viewwidth), contentmode: .aspectfill, options: self.requestoptions, resulthandler: { (image, _) in          println("\(asset.localidentifier) \(asset.creationdate) ")          [...]          self.tableview.reloaddata()     })  } 

this works have problem on every app start assets cached. right approach problem? how can display gallery photos in realtime whatsapp does?

refer photos framework example in project should file aaplassetgridviewcontroller.m , how handles caching based on scroll position.

 - (void)updatecachedassets         {             bool isviewvisible = [self isviewloaded] && [[self view] window] != nil;             if (!isviewvisible) { return; }              // preheat window twice height of visible rect             cgrect preheatrect = self.collectionview.bounds;             preheatrect = cgrectinset(preheatrect, 0.0f, -0.5f * cgrectgetheight(preheatrect));              // if scrolled "reasonable" amount...             cgfloat delta = abs(cgrectgetmidy(preheatrect) - cgrectgetmidy(self.previouspreheatrect));             if (delta > cgrectgetheight(self.collectionview.bounds) / 3.0f) {                  // compute assets start caching , stop caching.                 nsmutablearray *addedindexpaths = [nsmutablearray array];                 nsmutablearray *removedindexpaths = [nsmutablearray array];                  [self computedifferencebetweenrect:self.previouspreheatrect andrect:preheatrect removedhandler:^(cgrect removedrect) {                     nsarray *indexpaths = [self.collectionview aapl_indexpathsforelementsinrect:removedrect];                     [removedindexpaths addobjectsfromarray:indexpaths];                 } addedhandler:^(cgrect addedrect) {                     nsarray *indexpaths = [self.collectionview aapl_indexpathsforelementsinrect:addedrect];                     [addedindexpaths addobjectsfromarray:indexpaths];                 }];                  nsarray *assetstostartcaching = [self assetsatindexpaths:addedindexpaths];                 nsarray *assetstostopcaching = [self assetsatindexpaths:removedindexpaths];                  [self.imagemanager startcachingimagesforassets:assetstostartcaching                                                     targetsize:assetgridthumbnailsize                                                    contentmode:phimagecontentmodeaspectfill                                                        options:nil];                 [self.imagemanager stopcachingimagesforassets:assetstostopcaching                                                    targetsize:assetgridthumbnailsize                                                   contentmode:phimagecontentmodeaspectfill                                                       options:nil];                   self.previouspreheatrect = preheatrect;             }         }         //in case computedifference method changes handle horizontal scrolling             - (void)computedifferencebetweenrect:(cgrect)oldrect andrect:(cgrect)newrect removedhandler:(void (^)(cgrect removedrect))removedhandler addedhandler:(void (^)(cgrect addedrect))addedhandler             {                 if (cgrectintersectsrect(newrect, oldrect)) {                     cgfloat oldmaxy = cgrectgetmaxy(oldrect);                     cgfloat oldminy = cgrectgetminy(oldrect);                     cgfloat newmaxy = cgrectgetmaxy(newrect);                     cgfloat newminy = cgrectgetminy(newrect);                     if (newmaxy > oldmaxy) {                         cgrect recttoadd = cgrectmake(newrect.origin.x, oldmaxy, newrect.size.width, (newmaxy - oldmaxy));                         addedhandler(recttoadd);                     }                     if (oldminy > newminy) {                         cgrect recttoadd = cgrectmake(newrect.origin.x, newminy, newrect.size.width, (oldminy - newminy));                         addedhandler(recttoadd);                     }                     if (newmaxy < oldmaxy) {                         cgrect recttoremove = cgrectmake(newrect.origin.x, newmaxy, newrect.size.width, (oldmaxy - newmaxy));                         removedhandler(recttoremove);                     }                     if (oldminy < newminy) {                         cgrect recttoremove = cgrectmake(newrect.origin.x, oldminy, newrect.size.width, (newminy - oldminy));                         removedhandler(recttoremove);                     }                 } else {                     addedhandler(newrect);                     removedhandler(oldrect);                 }             }    - (nsarray *)assetsatindexpaths:(nsarray *)indexpaths     {         if (indexpaths.count == 0) { return nil; }          nsmutablearray *assets = [nsmutablearray arraywithcapacity:indexpaths.count];         (nsindexpath *indexpath in indexpaths) {             phasset *asset = self.assetsfetchresults[indexpath.item];             [assets addobject:asset];         }         return assets;     } 

the image caching manager heats assets of desired size , can retrieve them image caching manager itself.

hope helps you.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -