NSData *imageData = UIImageJPEGRepresentation(PhotoImage, 100);
NSString* queryString = ;
[SQLiteAccess insertWithSQL:
[NSString stringWithFormat:@"insert into images (photo) values ('%@')", imageData] // photo - blob column
]; // i use class SQLiteaccess
插入是正常的 但是当我从 sqlite 读取图像时
NSArray *photoSelectArray = [SQLiteAccess selectManyRowsWithSQL: [NSString stringWithFormat:@"select photo from places where id=%i", idPlace]];
NSDictionary *imageDataDic = [photoSelectArray objectAtIndex:0];
NSData *dataForCachedImage = [[NSData alloc] initWithData: [imageDataDic objectForKey:@"photo"]];
UIImage *cachedImage = [UIImage imageWithData:dataForCachedImage];
我有一个错误 - SIGABRT (由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__NSCFString bytes]:无法识别的选择器发送到实例 0x1e5c46d0')
附注轻松访问sqlite的类 - SQLiteAccess.h - http://pastebin.com/BFxFry7T - SQLiteAccess.m - http://pastebin.com/m67yNVLm
最佳答案
是的,我认为问题出在这里:
[NSString stringWithFormat:@"insert into images (photo) values ('%@')", imageData]];//我使用类 SQLiteaccess
当直接使用 SQLite 时,您应该通过 sqlite3_bind_blob()
设置二进制数据,但实际上您将二进制数据设置为字符串。
所以当你取回数据时,它不是二进制数据,它是一个String
对象,当然,一个字符串对象不响应-[NSData bytes]
与此同时,我认为您应该检查 SQLite 数据库文件中的字段类型。 photo
是否实际设置为 blob
。
关于ios - Objective-c 从 sqlite 读取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14746350/