UIImageã«æ ¼ç´ãããç»åã®ãã¤ããªã¤ã¡ã¼ã¸ãSQLiteã®DBã«ä¿åãããæ¹æ³
SQLiteã®ä½¿ãæ¹ã¯iPhoneSDKã§SQLiteã使ãæ¹æ³ã«æ¸ãã¦ããã¾ãã
ãµã³ãã«ã¨ãã¦ä½¿ããã¼ãã«
ãµã³ãã«ã¨ãã¦ä»¥ä¸ã®ãããªãã¼ãã«ãå®ç¾©ãã¦ãæ ¼ç´ãã¾ãã
ãã¤ããªãã¼ã¿ãæ ¼ç´ããé
ç®ã¯blobã§å®ç¾©ãã¾ãã
create table IMAGETABLE ( id integer, // ãªãã¨ãªãID size integer, // ãã¼ã¿ãµã¤ãº data blob // ãã¤ããªãã¼ã¿ );
ç»åãã¼ã¿ã®æ ¼ç´
UIImageã®ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã®UIImagePNGRepresentationã使ã£ã¦NSDataã®ã¤ã³ã¹ã¿ã³ã¹ã«å¤æãã¦ããããã¤ãé
åã¨ãã¦ãã¼ãã«ã«æ¸ãè¾¼ã¿ã¾ãã
ç»è³ªãããµã¤ãºãåªå
ãããªãã°ãUIImageJPEGRepresentationã§ããã¾ãã¾ããã
// ç»åãã¼ã¿ã¯sampleimageï¼UIImageåï¼ã«æ ¼ç´ããã¦ããã¨ãã¾ãã NSData *imagedata = [[NSData alloc] initWithData:UIImagePNGRepresentation(sampleimage)]; // SQL char *sqlImageInsert = "insert into IMAGETABLE values(?,?,?)"; // ã¯ã¨ãªã®å®è¡æ¹æ³çã¯å¥ã®ã¨ã³ããªã¼"iPhoneSDKã§SQLiteã使ãæ¹æ³(http://d.hatena.ne.jp/uosoft/20090619/1245341478)"åç § if(sqlite3_prepare_v2(database, sqlImageInsert, -1, &statement, NULL) == SQLITE_OK) { // ãã®å ´åãidã¯é©å½ sqlite3_bind_int(statement, 1, 1); // NSDataã®ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã使ã£ã¦ãµã¤ãºåå¾ããã sqlite3_bind_int(statement, 2, [imagedata length]); // NSDataã®ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã使ã£ã¦ãã¤ãé åããµã¤ãºããã©ã¡ã¼ã¿ã«è¨å®ãã¦sqlite3_bind_blobã§ã¯ã¨ãªã«ãã¤ã³ããã sqlite3_bind_blob(statement, 3, [imagedata bytes], [imagedata length], NULL); // å®è¡å¦ç sqlite3_step(statement); // çµäºå¦ç sqlite3_finalize(statement); } // ã¡ã¢ãªè§£æ¾ [imagedata release];
DBããç»åãã¼ã¿ãèªã
NSDataã®ã¯ã©ã¹ã¡ã½ããinitWithBytesã§DBãããã¤ããªãã¼ã¿ãåå¾ãã¾ãã
UIImageã®ã¯ã©ã¹ã¡ã½ããimageWithDataã«åå¾ãããã¤ããªãã¼ã¿ãæå®ãã¦UIImageã®ã¤ã³ã¹ã¿ã³ã¹ãä½æãã¾ãã
// ç»åãã¼ã¿ã¯sampleimageï¼UIImageåï¼ã«æ ¼ç´ããã¨ãã¾ãã UIImage *sampleimage; // SQL char *sqlImageSelect = "select size,data from IMAGETABLE where id=1"; // ã¯ã¨ãªã®å®è¡æ¹æ³çã¯å¥ã®ã¨ã³ããªã¼"iPhoneSDKã§SQLiteã使ãæ¹æ³(http://d.hatena.ne.jp/uosoft/20090619/1245341478)"åç § if(sqlite3_prepare_v2(database, sqlImageSelect, -1, &statement, NULL) == SQLITE_OK) { if (sqlite3_step(statement) == SQLITE_ROW) { // NSDataã®ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããinitWithBytesã§DBãããã¤ããªãã¼ã¿ãåå¾ NSData *imagedata = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 2) length:sqlite3_column_bytes(statement, 1)]; // UIImageã¤ã³ã¹ã¿ã³ã¹ã«åå¾ãããã¤ããªãæå®ãã¦åæå // ããã§sampleimageã«DBã«ä¿åããç»åãã¼ã¿ãæ ¼ç´ããã sampleimage = [[UIImage alloc] imageWithData:imagedata]; } }
iPhone ãããããã¼ãº ã¯ãã¯ããã¯
- ä½è : Erica Sadun,æ ªå¼ä¼ç¤¾ã¯ã¤ã¼ã
- åºç社/ã¡ã¼ã«ã¼: ã½ãããã³ã¯ã¯ãªã¨ã¤ãã£ã
- çºå£²æ¥: 2009/02/12
- ã¡ãã£ã¢: 大åæ¬
- è³¼å ¥: 20人 ã¯ãªãã¯: 474å
- ãã®ååãå«ãããã° (60件) ãè¦ã