ios - How will I stop repeating the loading of images in my table view? -
here code displaying images server. after image loaded, when scroll down , back, image disappears , there's delay before it's loaded again.
dispatch_async(kbgqueue, ^{ nsstring *profileimagestr = [[displayitems objectatindex:indexpath.row] objectforkey:@"image_url"]; nsurl* profileimgurl = [nsurl urlwithstring:profileimagestr]; nsdata *profileimgdata = [nsdata datawithcontentsofurl:profileimgurl]; nsstring *postphotostr = [[displayitems objectatindex:indexpath.row] objectforkey:@"image_url"]; nsurl *postimgurl = [nsurl urlwithstring:postphotostr]; nsdata *postimagedata = [nsdata datawithcontentsofurl:postimgurl]; dispatch_async(dispatch_get_main_queue(), ^{ if (postimagedata && profileimgdata != nil) { cell.thumbnailimg.image = [uiimage imagewithdata:profileimgdata]; cell.bgimgview.image = [uiimage imagewithdata:postimagedata]; }else { cell.thumbnailimg.image = [uiimage imagenamed:@"noimage.jpg"]; cell.bgimgview.image = [uiimage imagenamed:@"noimage.jpg"]; } }); } ); return cell;
i think need implement cache image. store image in nscache
.
//instance nscache *imagecache; - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { nsstring *simpletableidentifier=nil; if([objrepresentative.team count]>0) { simpletableidentifier=[nsstring stringwithformat:@"%@",[objrepresentative.employeeid objectatindex:indexpath.row]]; } else simpletableidentifier = @"contacttablecell"; contacttablecell *cell = (contacttablecell *)[tableview dequeuereusablecellwithidentifier:simpletableidentifier]; if (cell == nil) { nsarray *nib = [[nsbundle mainbundle] loadnibnamed:@"contacttablecell" owner:self options:nil]; cell = [nib objectatindex:0]; nsstring *photostring=[nsstring stringwithformat:@"http://www.foo.com/%@",[searchphoto objectatindex:indexpath.row]]; uiimage *image = [imagecache objectforkey:photostring]; if (image) { cell.profilepicture.image=image; } else { dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ nsurl *url = [nsurl urlwithstring:[photostring stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]]; uiimage *image = [[uiimage alloc] initwithdata:[nsdata datawithcontentsofurl:url]]; if(image) { dispatch_async(dispatch_get_main_queue(), ^{ uiimage *small = [self imagewithimage:image scaledtosize:cgsizemake(100, 100)]; cell.profilepicture.image=small; }); [imagecache setobject:image forkey:photostring]; } else cell.profilepicture.image=[uiimage imagenamed:@"nopicture.png"]; }); } } return cell; }
Comments
Post a Comment