2012-12-31 53 views
0

我剛開始開發一個像聯繫人一樣的ios應用程序。 它包括姓名,生日,羣組(學校,工作),血型,圖片等。在ios應用程序中保存和查看圖像

其中,其他數據可以通過鍵入來插入。 但是,圖像可以來自拍照,從Facebook下載..等

輸入圖像到xcode是不可能的。 Bcoz,另一個圖像可以一次又一次地來。

我該怎麼辦?請告訴我。

+0

如果應用商店與聯繫人一樣,應用商店不太可能批准您的iOS應用。 –

回答

1

你只需要添加libSqlite3.dylib到聯骨架和Lilbraries並宣佈數據庫.h文件中可變因素

//Database Variables 
    @property (strong, nonatomic) NSString *databasePath; 
    @property (nonatomic)sqlite3 *contactDB; 

拖放的UIImageView和名稱...我聲明imgView。

轉到.m文件你只是複製和粘貼代碼

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 


NSString *docsDir; 
NSArray *dirPaths; 

// Get the documents directory 
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = dirPaths[0]; 

// Build the path to the database file 
_databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"images.db"]]; 
//docsDir NSPathStore2 * @"/Users/gayathiridevi/Library/Application Support/iPhone Simulator/7.0.3/Applications/B5D4D2AF-C613-45F1-B414-829F38344C2A/Documents" 0x0895e160 
NSFileManager *filemgr = [NSFileManager defaultManager]; 

if ([filemgr fileExistsAtPath: _databasePath ] == NO) 
{ 
    const char *dbpath = [_databasePath UTF8String]; 

    if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) 
    { 
     char *errMsg; 
     const char *sql_stmt = "CREATE TABLE IF NOT EXISTS IMAGETB (ID INTEGER PRIMARY KEY AUTOINCREMENT,URL TEXT UNIQUE, IMAGE BLOB)"; 

     if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK) 
     { 

      NSLog(@"User table Not Created Error: %s", errMsg); 
     } 
     else 
     { 
      NSLog(@"User table Created: "); 
     } 
     sqlite3_close(_contactDB); 
    } 

    else { 

     NSLog(@"DB Not Created"); 
    } 
} 
[self saveImage]; 
[self showImage]; 
} 

- (void)saveImage 
{ 

sqlite3_stmt *statement; 
const char *dbpath = [_databasePath UTF8String]; 

if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) 
{ 

    NSString *[email protected]"INSERT INTO IMAGETB(image) VALUES(?)"; 

    if(sqlite3_prepare_v2(_contactDB, [insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL)== SQLITE_OK) 
    { 

     UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://lh6.googleusercontent.com/-vJBBGUtpXxk/AAAAAAAAAAI/AAAAAAAAADQ/nfgVPX1n-Q8/photo.jpg"]]]; 
     NSData *imageData=UIImagePNGRepresentation(image); 
     //sqlite3_bind_text(statement, 1, [@"" UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_blob(statement, 1, [imageData bytes], [imageData length], SQLITE_TRANSIENT); 
     NSLog(@"Length : %lu", (unsigned long)[imageData length]); 

    } 


    if (sqlite3_step(statement) == SQLITE_DONE) 
    { 
     NSLog(@"Insert into row id %lld",(sqlite3_last_insert_rowid(_contactDB))); 
    } 
    else { 

     NSLog(@"Error IN INSERT"); 
    } 
    sqlite3_finalize(statement); 
    sqlite3_close(_contactDB); 
    } 
} 

- (void)showImage 
{ 

sqlite3_stmt *statement; 
const char *dbpath = [_databasePath UTF8String]; 
int i = 1; 
if(sqlite3_open(dbpath,&_contactDB)==SQLITE_OK) 

{ 
    NSString *insertSQL = [NSString stringWithFormat:@"Select IMAGE FROM IMAGETB WHERE ID = %d",i]; 
    if(sqlite3_prepare_v2(_contactDB,[insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK) { 
     while(sqlite3_step(statement) == SQLITE_ROW) { 

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

      NSLog(@"Length : %lu", (unsigned long)[imageData length]); 

      if(imageData == nil) 
       NSLog(@"No image found."); 
      else 
       _imgView.image = [UIImage imageWithData:imageData]; 
      NSLog(@"image found."); 
     } 
    } 
    sqlite3_finalize(statement); 
} 
sqlite3_close(_contactDB); 
} 

它我的代碼。您修復了前進按鈕以通過更改i值來獲取詳細信息。 - (void)showImage 函數

+0

其可能的人...從相機拍攝照片到你的應用程序有很多方法... –

+0

請參閱[this](http://stackoverflow.com/questions/8726075/store-images-into-sqlite-database)通過更改Showimage()函數中的i值鏈接更改照片。 –

相關問題