正在回答
3回答
const char * sequel = "select image from imageTable where name=?";
sqlite3_stmt * getimg;
if (sqlite3_prepare_v2(database, sequel, -1, &getimg, NULL) == SQLITE_OK)
{
char *name = "imooc";
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];
}
}
sqlite3_finalize(getimg);
}可以参考如上代码实现数据的读取。
Tender10
2018-11-07 10:51:42
char *name = "imooc";
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 imooc_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(@"已经写入数据");
}
sqlite3_finalize(update);
}
}else{
NSLog(@"文件不存在");
}你再试一下上述方法,看是否有错。
Tender10
2018-11-06 18:34:11
其实就是转换成二进制数据,然后再保存到数据库中。
1、将图片文件imooc.png的二进制数据写到sqlite数据库,如下所示先进行转化,剩下的就是写入数据啦。
NSString * filePath = [[NSBundle mainBundle] pathForResource: @"imooc" ofType:@"png"];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])//判断图片是否存在
{
char *name="imooc";
NSData * imgData = UIImagePNGRepresentation([UIImage imageWithContentsOfFile:filePath]);
}相似问题
登录后可查看更多问答,登录/注册
iOS进阶:界面优化与数据存储
- 参与学习 516 人
- 提交作业 158 份
- 解答问题 637 个
本路径采用基础+案例方式,助你解开对界面优化、数据储存、屏幕适配的疑惑。6小时团购项目实战加最新版本Swift讲解,让你掌握更多iOS开发技巧。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星