iphone - Multiple images not showing from database -
i stored images in sqlitemanager local database. need show images in viewcontroller. when fetching images database showing 1 images. don't use webservice.
code:
sqlitemanager:
table name: simpletbl id sm descrip photo 1 sm1 ok blob(size:2345) 2 sm2 ok1 blob(size:3245) 3 sm3 ok2 blob(size:4535) .h file: @interface mysof : nsobject{ nsinteger sofid; nsstring *sof; nsstring *rating; uiimage *photo; } @property (nonatomic,retain)nsstring *sofa; @property (nonatomic, assign) nsinteger sofaid; @property (nonatomic, retain)nsstring *rating; @property (nonatomic, retain) uiimage *photo;
.m file:
- (nsmutablearray *) getmylists{ nsmutablearray *sarray = [[nsmutablearray alloc] init]; @try { nsfilemanager *filemgr = [nsfilemanager defaultmanager]; nsstring *dbpath = [[[nsbundle mainbundle] resourcepath ]stringbyappendingpathcomponent:@"sample.sqlite"]; bool success = [filemgr fileexistsatpath:dbpath]; if(!success) { nslog(@"cannot locate database file '%@'.", dbpath); } if(!(sqlite3_open([dbpath utf8string], &db) == sqlite_ok)) { nslog(@"an error has occured."); } ; const char *sql = "select * simpletbl"; nslog(@"sql %s", sql); sqlite3_stmt *sqlstatement; if(sqlite3_prepare(db, sql, -1, &sqlstatement, null) != sqlite_ok) { nslog(@"problem prepare statement"); } // while (sqlite3_step(sqlstatement)==sqlite_row) { mysof *mylist = [[mysof alloc]init]; mylist.sofid = sqlite3_column_int(sqlstatement, 0); mylist.sof = [nsstring stringwithutf8string:(char *) sqlite3_column_text(sqlstatement,1)]; mylist.rating = [nsstring stringwithutf8string:(char *) sqlite3_column_text(sqlstatement, 2)]; const char *raw = sqlite3_column_blob(sqlstatement, 3); int rawlen = sqlite3_column_bytes(sqlstatement, 3); nsdata *data = [nsdata datawithbytes:raw length:rawlen]; mylist.photo = [[uiimage alloc] initwithdata:data]; [sarray addobject:mylist]; } } @catch (nsexception *exception) { nslog(@"an exception occured: %@", [exception reason]); } @finally { return sarray; } }
then viewcontroller display fetching image imageview via button click:
-(void)click:(id)sender{ mmageview=[[uiimageview alloc]initwithframe:cgrectmake(20, 52, 72, 72)]; // uiimageview *mmageview=[uiimageview alloc]; // [mmageview setframe:cgrectmake(20, 52, 75, 75)]; [self.view addsubview:mmageview]; soflistsql * mysofs =[[soflistsql alloc] init]; self.arraysofs = [mysofs getmylists]; [mmageview setimage:((mysof *) [self.arraysofs objectatindex:0]).photo]; }
scrollview:
- (void)scrollviewdidscroll:(uiscrollview *)scrollview { cgpoint scrolloffset=scrollview.contentoffset; ///at time, have 3 pages loaded- previous, current , next if(pageonscrollview < ((int)scrolloffset.x/320)) { //unload if(pageonscrollview>1)[self unloadpreviouspage:pageonscrollview-2]; [self loadnextpage:((int)scrolloffset.x/320)+1]; else if(pageonscrollview > ((int)scrolloffset.x/320)) { //unload if(pageonscrollview<(num_pages-2))[self unloadpreviouspage:pageonscrollview+2]; [self loadnextpage:((int)scrolloffset.x/320)-1]; } pageonscrollview=scrolloffset.x/320; } -(void)unloadpreviouspage:(int)index { (int index = 0; index<[self.arraysofs count]; index++ ) { [[myscrollview viewwithtag:index+1] removefromsuperview]; } } -(void)loadnextpage:(int)index { int countflag=0; (int index = 0; index<[self.arraysofs count]; index++ ) { nslog(@"index %d",index); mmageview=[[uiimageview alloc]initwithframe:cgrectmake((320*index)+countflag*80+ 2, 5, 75, 75)]; [self.view addsubview:mmageview]; mmageview.tag=index+1; [mmageview setimage:((mysof *) [self.arraysofs objectatindex:index]).photo]; } [myscrollview addsubview:mmageview]; countflag++; }
1) log array count , check items count.
2) make loop this
for (int index = 0; index < [self.arraysofs count]; index ++ ) { nslog(@"index %d",index); mmageview=[[uiimageview alloc]initwithframe:cgrectmake(20+(index*74), 52, 72, 72)]; [self.view addsubview:mmageview]; soflistsql * mysofs =[[soflistsql alloc] init]; self.arraysofs = [mysofs getmylists]; [mmageview setimage:((mysof *) [self.arraysofs objectatindex:index]).photo]; }
check add images on scroll view adding uiimageviews uiscrollview
Comments
Post a Comment