I stored all the images in SQLiteManager for local database. After i added fetching images to array of images. Then add the array of images to UIButton for click event. Everything working fine on scrollview. But how to add click event separate images coming from database. I need to apply separate action for every image.
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)
4 sm4 ok3 BLOB(size:2545)
5 sm5 ok4 BLOB(size:3445)
6 sm6 ok5 BLOB(size:4535)
.h file:
@interface Mysof : NSObject{
NSInteger sofId;
NSString *sof;
NSString *rating;
UIImage *photo;
}
@property (nonatomic,retain)NSString *sof;
@property (nonatomic, assign) NSInteger sofId;
@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 * 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;
}
}
In viewcontroller i display the fetching image to UIButton for click event:
-(void)click:(id)sender{
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
for (int i = 0; i<[self.arraysofs count]; i++ ) {
NSLog(@"index %d",i);
imgView1=[[UIButton alloc]initWithFrame:CGRectMake(20+(i*74), 420, 72, 72)];
[imgView1 addTarget:self action:@selector(imgViewClicked:) forControlEvents:UIControlEventTouchUpInside];
[imgView1 setImage:((Mysof *)[self.arraysofs objectAtIndex:i]).photo forState:UIControlStateNormal];
[scroll addSubview:imgView1];
}
[scroll setContentSize:CGSizeMake(CGRectGetMaxX(imgView1.frame),self.view.frame.size.height)];
[self.view addSubview:scroll];
}
Click event for UIButton array of images:
-(void)imgViewClicked:(id)sender{
NSLog(@"button %d is clicked.", [sender tag]-1);
mmageView=[[UIImageView alloc]initWithFrame:CGRectMake(50,50,150,150)];
[mmageView setImage:((Mysof *) [self.arraysofs objectAtIndex:[sender tag]-1]).photo];
[self.view addSubview:mmageView];
UIPanGestureRecognizer *dbpan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(ondbPan:)];
[mmageView addGestureRecognizer:dbpan];
}
Pan function:
- (void)ondbPan:(UIPanGestureRecognizer *)pan {
CGPoint offset = [pan translationInView:mmageView];
CGPoint center = pan.view.center;
center.x += offset.x;
center.y += offset.y;
pan.view.center = center;
[pan setTranslation:CGPointZero inView:mmageView];
}