2012-06-05 102 views
4

我正在嘗試將圖像保存到Sqlite中,然後將圖像加載到UIImageView中。但它沒有得到工作。我不會發生什麼錯誤。這是我正在使用的代碼。任何人都可以幫助我解決這個問題。保存和檢索iOS和SQLite中的圖像

- (void)saveImage { 
    sqlite3_stmt *compiledStmt; 
    if(sqlite3_open([dbPath UTF8String], &db)==SQLITE_OK){ 
     const char *insertSQL="insert into Image(Image)values(?)"; 
     if(sqlite3_prepare_v2(db,insertSQL, -1, &compiledStmt, NULL) == SQLITE_OK){ 
      UIImage *image = [UIImage imageNamed:@"farmhouse.png"]; 
      NSData *imageData=UIImagePNGRepresentation(image); 
      sqlite3_bind_blob(compiledStmt, 1, [imageData bytes], [imageData length], NULL); 

      sqlite3_step(compiledStmt); 

      char *errMsg; 
      sqlite3_exec(db, insertSQL, NULL,compiledStmt,&errMsg); 

     } 
    } 
} 

- (void)showImage { 
    sqlite3_stmt *compiledStmt; 
    if(sqlite3_open([dbPath UTF8String], &db)==SQLITE_OK){ 
     const char *insertSQL = "Select Image from Image Where Sl.No = ?"; 
     sqlite3_prepare_v2(db,insertSQL, -1, &compiledStmt, NULL); 
     if(sqlite3_prepare_v2(db,insertSQL, -1, &compiledStmt, NULL) == SQLITE_OK){ 
      sqlite3_bind_int(compiledStmt, 1, 1); 
      if(SQLITE_DONE != sqlite3_step(compiledStmt)) {     
       NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStmt, 1) length:sqlite3_column_bytes(compiledStmt, 1)]; 

       if(data == nil) 
        NSLog(@"No image found."); 
       else 
        imgView.image = [UIImage imageWithData:data]; 


      } 
     } 
    } 
} 

任何人都可以用上面的代碼指出問題。

在此先感謝

+0

你爲什麼不使用文件系統呢? – Jake

+0

offtopic:對於未來的項目,你應該使用FMDatabse:https://github.com/ccgus/fmdb – CarlJ

回答

9
- (void)saveImage 
{ 
    sqlite3_stmt *compiledStmt; 
    sqlite3 *db; 
    if(sqlite3_open([dbPath UTF8String], &db)==SQLITE_OK){ 
    NSString *[email protected]"insert into Image(image) VALUES(?)"; 
    if(sqlite3_prepare_v2(db,[insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &compiledStmt, NULL) == SQLITE_OK){ 
    UIImage *image = [UIImage imageNamed:@"vegextra.png"]; 
    NSData *imageData=UIImagePNGRepresentation(image); 

    sqlite3_bind_blob(compiledStmt, 1, [imageData bytes], [imageData length], SQLITE_TRANSIENT); 

    if(sqlite3_step(compiledStmt) != SQLITE_DONE) { 
     NSLog(@"Error: %s", sqlite3_errmsg(db)); 
    } else { 
     NSLog(@"Insert into row id = %lld", (sqlite3_last_insert_rowid(db))); 
    } 

sqlite3_finalize(compiledStmt); 
    } 
} 
sqlite3_close(db); 
} 

- (void)showImage 
{ 
    sqlite3_stmt *compiledStmt; 
    sqlite3 *db; 
    int i = 1; 
    if(sqlite3_open([dbPath UTF8String], &db)==SQLITE_OK){ 
    NSString *insertSQL = [NSString stringWithFormat:@"Select image from Image Where Id = %d",i]; 
    if(sqlite3_prepare_v2(db,[insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &compiledStmt, NULL) == SQLITE_OK) { 
    while(sqlite3_step(compiledStmt) == SQLITE_ROW) { 

    int length = sqlite3_column_bytes(compiledStmt, 0); 
    NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(compiledStmt, 0) length:length]; 

    NSLog(@"Length : %d", [imageData length]); 

    if(imageData == nil) 
    NSLog(@"No image found."); 
    else 
    imgView.image = [UIImage imageWithData:imageData]; 
    } 
} 
sqlite3_finalize(compiledStmt); 
} 
sqlite3_close(db); 
} 
+0

我改變了代碼,如你所說,仍然無法正常工作。在sqlite中,圖像列包含數據爲「[BLOB]」。我不確定發生了什麼。 –

+0

嗨Hector,我用你的代碼替換了saveImage方法。但我收到以下錯誤:「錯誤:Image.Image可能不是NULL」。但我試圖將farmhouse.png的UIImage對象分配給UIImageView。有效。那爲什麼NULL錯誤越來越多。 –

+0

我該如何做PDF ...?我在本地有我的PDF .. –

相關問題