-2

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];
}
4

2 回答 2

1

currentImage只需使用方法从单击的按钮中获取图像。

-(void)imgViewClicked:(id)sender
{
    UIImage *image = [sender currentImage];
    // Show image where you want to show

    [self showImageOnCenter:image];
}

用于在中心显示图像

-(void)showImageOnCenter:(UIImage *)image
{
    // You can set the frame according to you and also implement the close functionality too
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 200.0, 200.0)];
    [imageView setImage:image];
    [imageView setCenter:self.view.center];
    [self.view addSubview:imageView];
}
于 2013-08-09T06:22:50.950 回答
0

试试这样,

-(void)imgViewClicked:(id)sender {

    UIButton *imgBtn = (UIButton *)sender;
    UIImage *img = [imgBtn imageForState:UIControlStateNormal];
}

或者

-(void)imgViewClicked:(UIButton *)sender {

    UIImage *img = [sender imageForState:UIControlStateNormal];
}
于 2013-08-09T06:43:04.130 回答