objective c - iOS - Downloading images asynchronously and using them alongside Core Data -


in core data model have entity keeps urls of images. want download these images can use them in uicollectionview model, array of core data entities. want able access these images synchronously (assuming have been downloaded async) there's no delay between them loading in respective cell.

currently, using async method in cellforindexpath: data source delegate method of uicollectionview when collection view reloaded, if there images still being downloaded, assigned wrong cell cells have been inserted during time.

this problem seems should obvious solve cannot work out.

any ideas appreciated.

thanks.

you can download images asynchronously in viewdidload, , add them nsmutablearray follows

dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{          (int = 0; < carphotos.count; i++) {              nsurl *photourl = [nsurl urlwithstring:carphotos[i]];             nsdata *photodata = [nsdata datawithcontentsofurl:photourl];             uiimage *image = [uiimage imagewithdata:photodata];             [carimages addobject:image];              dispatch_async(dispatch_get_main_queue(), ^{                 [self.tableview reloaddata];             });          }      }); 

and check in cellforrowatindexpath ensure indexpath matches arrayindex, , if want load dummy image not-loaded images, follows:

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath 

{ static nsstring *cellidentifier = @"cell"; carsappcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; cell.carmakelabel.text = carmakes[indexpath.row]; cell.carmodellabel.text = carmodels[indexpath.row];

if (carimages.count > indexpath.row) {     cell.carimage.image = carimages[indexpath.row]; } else {     cell.carimage.image = [uiimage imagenamed:@"dummy.png"]; }  return cell; 

}

i hope helps....


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -