2

我在数据库中存储了多个图像。我使用 SQLite 获取图像并将它们添加到数组中。当我单击按钮时,它会在滚动视图下显示图像数组。在滚动视图中,当我单击特定图像时,它会转到屏幕中心。具有另一个图像视图的屏幕图像的中心。当我从数组中删除特定图像时,当前图像不会被删除。

代码:

SQLite 管理器:

表名:SimpleTbl

  id    sm     descrip        photo
    1      sm1    ok            BLOB(size:2345)

    2      sm2    ok1            BLOB(size:3245)

    3      sm3    ok2            BLOB(size:4535)

.h 文件:

@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 我通过按钮单击将获取的图像显示到 imageview:

-(void)click:(id)sender{

for (int i = 0; i<[self.arraysofs count]; i++ ) {
            NSLog(@"index %d",i);



          //  imgView1=[[UIButton alloc]initWithFrame:CGRectMake(20+(i*74), 500, 72, 72)];

            imgView1=[[UIButton alloc]initWithFrame:CGRectMake(20+(i*74), 0, 72, 72)];

            Width = Width + 20+(i*74);

            [imgView1 setTag:i+1];

            [imgView1 addTarget:self action:@selector(arraysofsClicked:) forControlEvents:UIControlEventTouchUpInside];

            [imgView1 setImage:((Mysof *)[self.arraysofs objectAtIndex:i]).photo forState:UIControlStateNormal];

            [scrollview addSubview:imgView1];

          //  [myScroll addSubview:imgView1];



        }

}

它显示屏幕的中心:

-(void)arraysofsClicked:(id)sender{


 NSLog(@"button %d is clicked.", [sender tag]-1);



    mmageView=[[UIImageView alloc]initWithFrame:CGRectMake(50,50,150,150)];

    [mmageView setUserInteractionEnabled:YES];

     [mmageView setImage:((Mysof *) [self.arraysofs objectAtIndex:[sender tag]-1]).photo];

    [self.view addSubview:mmageView];

UILongPressGestureRecognizer *dblongpress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(dblongPress:)];

    [mmageView addGestureRecognizer:dblongpress];

}

长按从屏幕中央删除图像:

-(void)dblongPress:(UILongPressGestureRecognizer*)sender{

   //  NSLog(@"button %d is clicked.", [send tag]-1);

   // [mmageView setImage:((Mysof *) [self.sofas objectAtIndex:[send tag]-1]).photo];

    [mmageView removeFromSuperview];

  }

如果屏幕中央有两个图像,当我单击第一个图像时,第二个图像将被删除。

4

1 回答 1

0

您删除后看到的图像视图是您之前添加的较旧的图像视图。这是因为每次按下按钮时,它都会调用 -(void)arraysofsClicked,并在那里添加一个新的 imageview 实例。

解决方案是:

把它放在 viewDidLoad

mnageView = nil;

&更改此方法

-(void)arraysofsClicked:(id)sender{

// chcek is mnageView is initialized before or not
if(mmageView == nil) {

    mmageView=[[UIImageView alloc]initWithFrame:CGRectMake(50,50,150,150)];

    [mmageView setUserInteractionEnabled:YES];


    [self.view addSubview:mmageView];

    UILongPressGestureRecognizer *dblongpress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(dblongPress:)];

    [mmageView addGestureRecognizer:dblongpress];

}


// change image here
[mmageView setImage:((Mysof *) [self.arraysofs objectAtIndex:[sender tag]-1]).photo];

}

如果您不这样做,请全局声明 mnageView。但从您的代码看来,您是在全局声明它。

于 2013-09-05T09:35:49.133 回答