2011-06-28 496 views
0

我將圖像轉換爲數據並在數據庫中保存圖像如下 NSData * imageData = UIImagePNGRepresentation(imgView.image);如何保存並從Sqlite數據庫檢索圖像?

[dataArray addObject:imageData]; 

和檢索數據到相關圖片如下

 NSData *imdata = [[tableArray objectAtIndex:indexPath.row] objectForKey:@"Image"]; 

SQlite的代碼保存:

if (sqlite3_open([databasePath UTF8String], &database)== SQLITE_OK) 
    { 
     NSString *statement; 
     sqlite3_stmt *compiledstatement; 
     int Id; 
     NSString *Name; 
     NSData *imgData; 



     Id = [[recordArray objectAtIndex:0] intValue]; 
     Name = [recordArray objectAtIndex:1]; 

     imgData = [recordArray objectAtIndex:2]; 
     statement = [[NSString alloc]initWithFormat:@"insert into ImageTable values ('%d','%@','%@')",Id,Name,imgData] ; 
     const char *sqlstatement = [statement UTF8String]; 

     if (sqlite3_prepare_v2(database, sqlstatement, -1, &compiledstatement, NULL)== SQLITE_OK) { 
      if (SQLITE_DONE!=sqlite3_step(compiledstatement)) { 
       NSAssert1(0,@"Error when inserting %s",sqlite3_errmsg(database)); 
      } 
      else { 

       NSLog(@"Data inserted Successfully"); 
      } 
      //[recordDict release]; 
     } 
     else { 
      NSLog(@"Failed with error"); 
      NSAssert1(0,@"Error when creation %s",sqlite3_errmsg(database)); 

     } 



    } 
    //sqlite3_finalize(compiledstatement); 

sqlite的代號爲獲得:

- (NSMutableArray *) getRecord 
    { 
[self checkAndCreateDatabase]; 

sqlArray=[[NSMutableArray alloc]init ]; 


if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
{ 
    const char *sql = "select * from ImageTable"; 
    sqlite3_stmt *selectstmt; 
    if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) 
    { 
     while(sqlite3_step(selectstmt) == SQLITE_ROW) 
     { 
      //[sqlDict retain]; 
      sqlDict =[[NSMutableDictionary alloc]init ]; 

      NSString *Id = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)]; 
      //[barcodeArray addObject:prdbcode]; 
      [sqlDict setObject:Id forKey:@"Id"]; 
      //[prdbcode release]; 


      NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]; 
      //[nameArray addObject:prdname]; 
      [sqlDict setObject:name forKey:@"Name"]; 
      //[prdname release]; 


      NSData *image = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)]; 
      //NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(selectstmt, 1) length:sqlite3_column_bytes(selectstmt, 1)];    
      //[descArray addObject:prdDesc]; 
      [sqlDict setObject:image forKey:@"Image"]; 
      // [prdDesc release]; 
      [sqlArray addObject:sqlDict]; 
     } 


     sqlite3_finalize(selectstmt); 

    } 

    sqlite3_close(database); 
    // 
} 

return sqlArray; 

}

[imView setImage:[UIImage imageWithData:imdata]]; 
[cell addSubview:imView]; 

Id和Name被檢索但圖像沒有顯示。 應用程序終止,沒有任何錯誤消息。 請讓我知道這個解決方案

+0

這與SQLite有什麼關係? –

+0

類似的問題: http://stackoverflow.com/questions/10192945/to-reterive-blob-image-from-sqlite 希望你得到你的問題就解決了。 – anjum

回答

0

我沒有看到任何sqlite數據庫代碼。如果你只是想拉出來的圖像你dataArray中,你想:

NSData *imdata = [dataArray objectAtIndex:indexPath.row]; 
1

我知道我的回答是爲時已晚,但它可能是一些一誰針對這一問題非常有用,通常同時stroring圖片sqlite數據庫表使用BLOB,這將對數據庫的性能產生影響,並會減慢數據的載入,當記錄開始增加數量時。無需使用BLOB,可以從數據庫中保存和檢索圖像,這裏是post/link這將幫助你繼續和實現任務

希望它可以幫助一些誰與數據庫中的圖像存儲感到震驚:)

相關問題