2012-06-05 5 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에서 Image 열은 "[BLOB]"로 데이터를 포함합니다. 나는 무슨 일이 일어나고 있는지 잘 모르겠다. –

+0

안녕하세요, 저는 saveImage 메서드를 코드로 대체했습니다. 하지만 다음과 같은 오류가 발생합니다 : "오류 : Image.Image가 NULL이 아닐 수도 있습니다". 하지만 UIImageView에 farmhouse.png의 UIImage 객체를 할당하려고했습니다. 그것은 효과가 있었다. 그렇다면 NULL 오류가 발생하는 이유. –

+0

PDF의 경우 어떻게해야합니까? 나는 현지에서 나의 PDF를 가지고있다 .. –

관련 문제