ios - Odd threading issue with UITableView images loaded from URL -
first i'm ui designer getting xcode, please bear me if i'm little vague in description here.
basically have uitableview total of contacts, , each contact has photo image left of name, pulled web via url.. have implemented lazy loading images, when scroll through list, seem getting odd flickering , 'laggy' (ie. not smooth) scroll.
i have tried playing around setneedsdisplay
calls (i know there's lot of them), still seems wig out.
have been trying google 'best practices' this, not sure what's going on here... help appreciated! thanks.
target: ios8 in xcode 6.1
here's code cellforrowatindexpath:
if([peep getimage].length > 4) // if url smaller 4, it's not valid url { dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ if([peep getimage]) { if(![imagecache objectforkey:indexpath]) { dispatch_async(dispatch_get_main_queue(), ^{ uiimage *image = [uiimage imagewithdata:[nsdata datawithcontentsofurl:[nsurl urlwithstring:[peep getimage]]]]; if(image != nil) { [imagecache setobject:image forkey:indexpath]; [cell.peepimage setimage:[imagecache objectforkey:indexpath]]; [[cell peepimage] setneedsdisplay]; } }); } else { dispatch_async(dispatch_get_main_queue(), ^{ [[cell peepimage] setimage:[imagecache objectforkey:indexpath]]; [[cell peepimage] setneedsdisplay]; }); } } [cell.peepimage setneedsdisplay]; }); } else { cell.peepimage.image = [uiimage imagenamed: @"defaultuser.jpg"]; } [cell setneedsdisplay]; homecoming cell;
}
my guess creation of image on main thread. moving off main thread problem should fixed.
if([peep getimage].length > 4) // if url smaller 4, it's not valid url { dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ if([peep getimage]) { if(![imagecache objectforkey:indexpath]) { // move image creation here __block uiimage *image = [uiimage imagewithdata:[nsdata datawithcontentsofurl:[nsurl urlwithstring:[peep getimage]]]]; dispatch_async(dispatch_get_main_queue(), ^{ if(image != nil) { [imagecache setobject:image forkey:indexpath]; [cell.peepimage setimage:[imagecache objectforkey:indexpath]]; [[cell peepimage] setneedsdisplay]; } }); } else { dispatch_async(dispatch_get_main_queue(), ^{ [[cell peepimage] setimage:[imagecache objectforkey:indexpath]]; [[cell peepimage] setneedsdisplay]; }); } } [cell.peepimage setneedsdisplay]; }); } else { cell.peepimage.image = [uiimage imagenamed: @"defaultuser.jpg"]; } [cell setneedsdisplay]; homecoming cell;
ios objective-c xcode uitableview lazy-loading
No comments:
Post a Comment