Use SQLite database to store pictures under iOS, convert your picture to NSData form, and add a row of BLOB data to the database
Assuming that table test_table (Name,image) exists in the database, the following code writes the binary data of the picture file Test.png to the SQLite database:
CHAR *name = "Test";
NSString * namestring = [NSString stringwithcstring:name encoding:nsutf8stringencoding];
NSString * FilePath = [[NSBundle mainbundle] pathforresource:namestring oftype:@ "PNG"];
IF ([[[Nsfilemanager Defaultmanager] Fileexistsatpath:filepath])
{
NSData * Imgdata = uiimagepngrepresentation ([uiimage Imagewithcontentsoffile:filepath]);
Const CHAR * sequel = "INSERT Into test_table (name,image) VALUES (?,?)";
SQLITE3_STMT * UPDATE;
IF (Sqlite3_prepare_v2 (DATABASE, sequel,-1, &update, NULL) = = SQLITE_OK)
{
Sqlite3_bind_text (UPDATE, 1, name,-1, NULL);
Sqlite3_bind_blob (UPDATE, 2, [imgdata bytes], [Imgdata LENGTH], NULL);
IF (Sqlite3_step (UPDATE) = = Sqlite_done)
{
NSLog (@ "already write data");
}
Sqlite3_finalize (UPDATE);
}
}
ELSE
{
NSLog (@ "file does not exist");
}
The following code reads the picture binary data from the database and then displays the picture:
Const CHAR * sequel = "Select Image from Test_table where name=?";
SQLITE3_STMT * GETIMG;
IF (Sqlite3_prepare_v2 (DATABASE, sequel,-1, &getimg, NULL) = = SQLITE_OK)
{
CHAR *name = "Test";
Sqlite3_bind_text (UPDATE, 1, name,-1, NULL);
IF (Sqlite3_step (getimg) = = Sqlite_row)
{
INT bytes = sqlite3_column_bytes (getimg, 0);
Byte * VALUE = (byte*) Sqlite3_column_blob (getimg, 1);
IF (bytes!=0 && VALUE!= NULL)
{
NSData * DATA = [NSData datawithbytes:value length:bytes];
UIImage * img = [uiimage imagewithdata:data];
Uiimageview * Aview =[[uiimageview alloc] initWithFrame:
CGRectMake (0.0, 0.0, image_width, image_height)];
Aview.image = img;
[SELF. VIEW Addsubview:aview];
[Aview release];
}
}
Sqlite3_finalize (GETIMG);
}
Example 2: Storing pictures
/Save Small Image Data by given main URL
-(void) Saveimagestosql: (nsdata*) Imgdata:(nsstring*) mainurl
{
NSLog (@ "N*****save image to Sqlite*****n");
Const char* Sqlitequery = "INSERT into IMAGES (URL, IMAGE) VALUES (?,?)";
sqlite3_stmt* statement;
if (Sqlite3_prepare_v2 (Articlesdb, Sqlitequery,-1, &statement, NULL) = = SQLITE_OK)
{
Sqlite3_bind_text (statement, 1, [Mainurl utf8string],-1, sqlite_transient);
Sqlite3_bind_blob (statement, 2, [imgdata bytes], [imgdata length], sqlite_transient);
Sqlite3_step (statement);
}
else NSLog (@ "savebody:failed from SQLITE3_PREPARE_V2.) Error is:%s, sqlite3_errmsg (ARTICLESDB));
Finalize and close database.
Sqlite3_finalize (statement);
}
Read pictures:
Load images from the data base with given image URL
-(nsdata*) Loadimagesfromsql: (nsstring*) ImageLink
{
nsdata* data = nil;
nsstring* sqlitequery = [NSString stringwithformat:@ "select IMAGE from IMAGES WHERE URL = '%@ '", ImageLink];
sqlite3_stmt* statement;
if (Sqlite3_prepare_v2 (Articlesdb, [Sqlitequery utf8string],-1, &statement, NULL) = = SQLITE_OK)
{
if (sqlite3_step (statement) = = Sqlite_row)
{
int length = sqlite3_column_bytes (statement, 0);
data = [NSData datawithbytes:sqlite3_column_blob (statement, 0) length:length];
}
}
Finalize and close database.
Sqlite3_finalize (statement);
return data;
}