ios - Loading images from file to UICollectionView, using GCD -
when take picture, save image file in folder made in nsdocuments directory. (/documents/photos/....png)
then load photos in photo's folder in collection view. since files big, decided use grand central dispatch perform grabbing image on background thread. when scroll , down uicollectionview can see images changing randomly before displaying correct image. assume whenever grab image set on main thread, don't know how deal this.
- (uicollectionviewcell *)collectionview:(uicollectionview *)cv cellforitematindexpath:(nsindexpath *)indexpath { nsuinteger rownumber = [indexpath row]; //nsstring *imagepath = [self.photopatharray objectatindex:rownumber]; nsarray *directorycontent = [[nsfilemanager defaultmanager] contentsofdirectoryatpath:[[nshomedirectory() stringbyappendingpathcomponent:@"documents"] stringbyappendingpathcomponent:@"photos"] error:null]; nsstring *imagepath = [nshomedirectory() stringbyappendingformat:@"/documents/photos/%@",[directorycontent objectatindex:rownumber]]; nslog(@"the image path %@", imagepath); imagecell *cell = [cv dequeuereusablecellwithreuseidentifier:@"imagecell" forindexpath:indexpath]; dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ uiimage *originalimage = [uiimage imagewithcontentsoffile:imagepath]; uiimage *thumbnail = [self shrinkimage:originalimage withsize:cgsizemake(200, 200)]; dispatch_sync(dispatch_get_main_queue(), ^{ cell.imageview.image = thumbnail; }); }); return cell; }
check out apple's sample app: lazytableimages. incorporated collection view.
http://developer.apple.com/library/ios/#samplecode/lazytableimages/introduction/intro.html
Comments
Post a Comment