すべての画像をローカル データベースの SQLiteManager に保存しました。ビューコントローラーにすべての画像を表示する必要があります。しかし、データベースから画像を取得していたときに、画像が 1 つしか表示されませんでした。私はWebサービスを使用していません。
コード:
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 ファイル:
- (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 * FROM SimpleTbl";
NSLog(@"sql is %s", sql);
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with 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;
}
}
次に、viewcontroller i は、フェッチした画像をボタン クリックで imageview に表示します。
-(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];
}
スクロールビュー:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint scrollOffset=scrollView.contentOffset;
///at any time, it will have only 3 pages loaded- previous, current and 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
{
for (int index = 0; index<[self.arraysofs count]; index++ ) {
[[myScrollView viewWithTag:index+1] removeFromSuperview];
}
}
-(void)loadNextPage:(int)index
{
int countFlag=0;
for (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++;
}