我将图像存储在远程服务器中。然后我使用 JSON 框架从数据库中获取数据。我使用 NSLog 它显示 ID、名称、图像名称。但是没有显示图像。我用于BLOB
在本地存储图像。但是当我将数组用于 NSLog 时,它会显示一些加密值。
代码:
sofArray = [[NSMutableArray alloc] init];
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSError *err;
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Empty" ofType:@"sqlite"];
//NSLog(@"bundlePath %@", bundlePath);
//call update function to check any data updated,
//if there is a version difference
//update the data base with all the required fileds.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//NSLog(@"docs dir is %@", documentsDirectory);
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"App10.sqlite"];
[fileMgr copyItemAtPath:bundlePath toPath:appFile error:&err];
NSURL *URL = [NSURL URLWithString:@"http://server.net/projects/mobile/jsonstring.php"];
NSLog(@"URL is %@", URL);
NSError *error;
NSString *stringFromFileAtURL = [[NSString alloc]
initWithContentsOfURL:URL
encoding:NSUTF8StringEncoding
error:&error];
//NSLog(@"response is %@", stringFromFileAtURL);
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"App10.sqlite"];
NSArray *userData = [stringFromFileAtURL JSONValue];
// NSArray *skarray = [[NSArray alloc]init];
NSLog(@"userdata is %@", userData);
// int i = 0;
BOOL notExist = TRUE;
for (NSArray *skarray in userData) {
for (NSDictionary *tuser in skarray) {
// if (sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK) {
if (sqlite3_open([path UTF8String], &db) == SQLITE_OK) {
// const char *sql = "SELECT * FROM categories";
// const char *sql = [[NSString stringWithFormat:@"SELECT id FROM categories where cat_id = '%@'",[tuser objectForKey:@"cat_id"]] cStringUsingEncoding:NSUTF8StringEncoding];
const char *sql = [[NSString stringWithFormat:@"SELECT id FROM categories where id = '%@'",[tuser objectForKey:@"id"]] cStringUsingEncoding:NSUTF8StringEncoding];
//NSLog(@"check stmt is %s", sql);
sqlite3_stmt *sqlStatement,*addStmt;
if (sqlite3_prepare_v2(db, sql, -1, &sqlStatement, NULL) == SQLITE_OK) {
notExist = TRUE;
while (sqlite3_step(sqlStatement) == SQLITE_ROW) {
notExist = FALSE;
Mysof *Mylist = [[Mysof alloc]init];
Mylist.sofaId = sqlite3_column_int(sqlStatement, 0);
Mylist.sofa = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
Mylist.rating = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 2)];
const char *raw = sqlite3_column_blob(sqlStatement, 3);
int rawLen = sqlite3_column_bytes(sqlStatement, 3);
NSData *data = [NSData dataWithBytes:raw length:rawLen];
Mylist.photo = [[UIImage alloc] initWithData:data];
[sofArray addObject:Mylist];
}
if(notExist){
//NSLog(@"cat id does not exist");
const char *sqlInsert = [[NSString stringWithFormat:@"insert into categories (id, cat_name,order_by) values ('%@', '%@', '%@')", [tuser objectForKey:@"id"], [tuser objectForKey:@"cat_name"],[tuser objectForKey:@"order_by"]] cStringUsingEncoding:NSUTF8StringEncoding];
//NSLog(@"stmt is %s", sqlInsert);
if(sqlite3_prepare_v2(db, sqlInsert, -1, &addStmt, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(db));
if(SQLITE_DONE != sqlite3_step(addStmt))
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(db));
}
}
}
}
}
return sofArray;