uitableview - How to load table with some default image till it get the original image from internet iPhone? -
uitableview - How to load table with some default image till it get the original image from internet iPhone? -
i have uitableview stores 1 image in 1 cell. image loaded internet. problem application slow till image loaded internet. want set mechanism first loaded default image , when gets original default image replaced original one.
give me tutorial or sample code it.
thanks in advance.
get image trough other thread .... , utilize notification or delegate maintain track of alter in image download ... suppose using notification .... in class set observer notification reload info of tableview .... table image updated there default image .... more detail lets take illustration ..... create operation class downloading image phone call thumb .... in illustration create 2 classes 1. photogalleryvc shows list of thumbs , detail of each thumb , 2. loadgallerythumbop [op = operation] downloads thumbs , post notification when done
@protocol loadgallerythumbdelegate; @interface loadgallerythumbop : nsobject{ nsindexpath* indexpathintableview; id <loadgallerythumbdelegate> delegate; nsmutabledata *activedownload; nsurlconnection *imageconnection; nsstring * documentpath; bool imagedownload; } @property (nonatomic, assign) nsindexpath* indexpathintableview; @property (nonatomic, retain) nsmutabledata *activedownload; @property (nonatomic, retain) nsurlconnection *imageconnection; @property (nonatomic, assign) id <loadgallerythumbdelegate> delegate; @property (nonatomic, retain) nsstring * documentpath; @property (nonatomic) bool imagedownload; - (void)startdownload; - (void)canceldownload; - (void) persistdata:(nsdata*) data; @end @protocol loadgallerythumbdelegate - (void)appimagedidload:(nsindexpath *)indexpath; @end in loadgallerythumbop.m as
@implementation loadgallerythumbop @synthesize year; @synthesize indexpathintableview; @synthesize delegate; @synthesize activedownload; @synthesize imageconnection,documentpath,imagedownload; #pragma mark - (void)startdownload { self.imagedownload = yes; self.activedownload = [nsmutabledata data]; nsfilemanager* fm = [nsfilemanager defaultmanager]; nsstring* gallerydocumentpath = [self.documentpath stringbyappendingpathcomponent:[nsstring stringwithformat:@"images/thumb.jpg"]]; if ([fm fileexistsatpath:gallerydocumentpath]) { uiimage *image = [[uiimage alloc] initwithcontentsoffile:gallerydocumentpath ]; self.gallery.thumpimage = image; self.activedownload = nil; [image release]; self.imageconnection = nil; [delegate appimagedidload:self.indexpathintableview]; } else { nsurlconnection *conn = [[nsurlconnection alloc] initwithrequest: [nsurlrequest requestwithurl: [nsurl urlwithstring:#error yourimageurl]] delegate:self]; self.imageconnection = conn; [conn release]; } } - (void)canceldownload { [self.imageconnection cancel]; self.imageconnection = nil; self.activedownload = nil; } #pragma mark - #pragma mark download back upwards (nsurlconnectiondelegate) - (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { [self.activedownload appenddata:data]; } - (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error { self.activedownload = nil; self.imageconnection = nil; self.imagedownload = no; [nsstring stringwithformat:@"images/thumb.jpg"]]; [delegate appimagedidload:self.indexpathintableview]; } - (void)connectiondidfinishloading:(nsurlconnection *)connection { [self persistdata:self.activedownload]; self.activedownload = nil; self.imageconnection = nil; [delegate appimagedidload:self.indexpathintableview]; } - (void) persistdata:(nsdata*) info { nsfilemanager* fm = [nsfilemanager defaultmanager]; nsstring* gallerydocumentpath = [self.documentpath stringbyappendingpathcomponent:[nsstring stringwithformat:@"images/thumb.jpg"]]; if ([[nsfilemanager defaultmanager] fileexistsatpath:gallerydocumentpath]) { nserror* err = nil; [fm removeitematpath:gallerydocumentpath error:&err]; if (err) nslog(@"%s:%@",__function__,err); } [fm createfileatpath:gallerydocumentpath contents:data attributes:nil]; } this class download image wanted , phone call delegate when image download .. comes utilize part in photogalleryvc utilize this
@interface photogalleryvc : uiviewcontroller <loadgallerythumbdelegate>{ iboutlet uitableview* albumlisttableview; nsmutabledictionary *imagedownloadsinprogress; nsarray* allthumbs; } @property (nonatomic, retain) nsmutabledictionary *imagedownloadsinprogress; - (void)appimagedidload:(nsindexpath *)indexpath; @end in .m part
- (void)viewdidload { self.imagedownloadsinprogress = [nsmutabledictionary dictionary]; ..... } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { nsfilemanager* fm = [nsfilemanager defaultmanager]; nsstring* gallerydocumentpath = [[nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0] stringbyappendingpathcomponent:[nsstring stringwithformat:@"gallery/%@/images/thumb.jpg",[someobj.title stringbyreplacingoccurrencesofstring:@" " withstring:@"_" ]]]; //this above line create seperate folder each object , store thumbs of object in folder ... create unique how if (![fm fileexistsatpath:gallerydocumentpath]) { loadgallerythumbop *gallerythumbdownloader = [imagedownloadsinprogress objectforkey:indexpath]; if (gallerythumbdownloader != nil && gallerythumbdownloader.imagedownload == no) { [cell.activityindicator stopanimating]; cell.albumcoverimageview.image = [uiimage imagenamed:@"no_thumb.png"]; } else { [cell.activityindicator startanimating]; } [self starticondownload:temp forindexpath:indexpath andyear:[nsstring stringwithformat:@"%d",selectedyear]]; } else { [cell.activityindicator stopanimating]; cell.albumcoverimageview.image = [uiimage imagewithcontentsoffile:gallerydocumentpath]; } homecoming cell ; } //the next method see if there downloader downloading same image nil else create downloader , start - (void)starticondownload:(gallery *)gallery forindexpath:(nsindexpath *)indexpath andyear:(nsstring*)yr { loadgallerythumbop *gallerythumbdownloader = [imagedownloadsinprogress objectforkey:indexpath]; if (gallerythumbdownloader == nil) { nsstring* documentspath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring* namewithoutspace = [gallery.title stringbyreplacingoccurrencesofstring:@" " withstring:@"_" ]; nsstring* gallerydocumentpath = [documentspath stringbyappendingpathcomponent:[nsstring stringwithformat:@"gallery//%@",namewithoutspace]]; gallerythumbdownloader = [[loadgallerythumbop alloc] init]; gallerythumbdownloader.documentpath = gallerydocumentpath; gallerythumbdownloader.indexpathintableview = indexpath; gallerythumbdownloader.delegate = self; [imagedownloadsinprogress setobject:gallerythumbdownloader forkey:indexpath]; [gallerythumbdownloader startdownload]; [gallerythumbdownloader release]; } else if(gallerythumbdownloader.imagedownload == no) { if (albumlisttableview.dragging || albumlisttableview.decelerating) { [gallerythumbdownloader startdownload]; } } } finally method called when particular image downloaded
- (void)appimagedidload:(nsindexpath *)indexpath { customcellphotogalary* cell = (customcellphotogalary*)[albumlisttableview cellforrowatindexpath:indexpath]; loadgallerythumbop *gallerythumbdownloader = [imagedownloadsinprogress objectforkey:indexpath]; if (gallerythumbdownloader != nil) { if (gallerythumbdownloader.imagedownload) { [cell.activityindicator stopanimating]; nsstring* documentspath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring* namewithoutspace = [temp.title stringbyreplacingoccurrencesofstring:@" " withstring:@"_" ]; nsstring* gallerydocumentpath = [documentspath stringbyappendingpathcomponent:[nsstring stringwithformat:@"gallery/%@",namewithoutspace]]; cell.albumcoverimageview.image = [uiimage imagewithcontentsoffile:gallerydocumentpath]; //gallerythumbdownloader = nil; } else { [cell.activityindicator stopanimating]; cell.albumcoverimageview.image = [uiimage imagenamed:@"no_thumb.png"]; [self starticondownload:temp forindexpath:indexpath andyear:[nsstring stringwithformat:@"%d",selectedyear]]; } [albumlisttableview reloaddata]; } } woooh.....! thats lot of code 1 illustration note : cutting out many line code may not work u straight {there may many errors .. }, hope main thought behind scene ... :)
iphone uitableview uiimageview default-value
Comments
Post a Comment