2011-07-04 6 views
1

こんにちは私は自分のコンピュータに作成されたSqlite3データベースを持っていて、私のiPhoneでそのデータベースを読みたいと思っています。 次のコードはIphoneシミュレータでは動作しますが、実際のデバイスでは動作しません。デバイスアーキテクチャ(MacBookとIphone)に関連する問題です。 問題は、データを読み込んでUIImageに変換しようとすると、結果がUIImageViewが画像の表示ではなく空白になるため、データが正しくないように見える場合です。シミュレータでうまく動作します。Sqlite3 Blob to UIImage

-(IBAction) findPhoto{ 

databaseName = @"memoryDB.sqlite"; 

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName]; 



    int res = 0; 

    res = sqlite3_open_v2([databasePath UTF8String], &database, SQLITE_OPEN_READONLY, nil); 

    if(res == SQLITE_OK) { 
     // Setup the SQL Statement and compile it for faster access 

     NSString *querySQL = [NSString stringWithString:@"SELECT * FROM Fotos"]; 

     const char *sqlStatement = [querySQL UTF8String]; 

     sqlite3_stmt *compiledStatement; 
     if((res = sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL)) == SQLITE_OK) { 
      // Loop through the results and add them to the feeds array 
      while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
       // Read the data from the result row 


       UIImage *image; 

       NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 1) length:sqlite3_column_bytes(compiledStatement, 1)]; 

       if(data == nil) 
        NSLog(@"No image found."); 
       else 
        image = [[UIImage alloc] initWithData:data];  
       // 

       NSLog(@"width: %f,height: %f",image.size.width, image.size.height); 

       UIImageView *imgView = [[UIImageView alloc] initWithImage:image]; 
       [self.view addSubview:imgView]; 

      } 
     } 
     // Release the compiled statement from memory 
     sqlite3_finalize(compiledStatement); 
     sqlite3_close(database); 

    } 
} 

私はプロジェクトにデータベース(DB)を含め、DBは自分のMacBookに作成されました。それは問題ですか?データを正しい形式に変換することは可能ですか?

答えて

-1

これは適切な方法ではありません。データベースに画像を保存する。

NSDocumentDirectoryに保存する必要があります。

dbのテーブルに画像の名前を保存するだけです。

だから、テーブルからイメージ名の名前を取得し、documentdirectoryからイメージを取得するのは簡単です。データベースから非常に速くイメージを取得します。

+0

ありがとうございます。私はそれを試みますが、私はアプリケーションのサイズにいくつかの問題があります。私は20 MB未満にしたい。あなたは私が写真へのパスを持っていることをお勧めしますそれは写真を持っているのではなく... –

+0

あなたがそれを保存するならば、それはまたDBのサイズを増加させます。 –

+0

生のバイナリデータをdbに保存するのは間違っているとは思わない。そのため、BLOB(およびTINYBLOB、MEDIUMBLOBなど)が存在します。それには欠点がありますが、時には役立ちますが、場合によっては使用すべきリソースです。 – Daniel

関連する問題