从数据库检索NSData时,收到发送给__NSCFString的未识别选择器错误。

3

我在将 NSData 转换为 UIImage 时遇到了问题。

我从设备相机中捕获图像,并将该图像转换为NSData以使用BLOB数据类型将其存储在SQLite中。

图像数据已成功存储在数据库中,但当我检索我的图像数据时,应用程序会崩溃。

我使用以下代码:

NSData *tempData = [[NSData alloc] init];
tempData = [[arr_img objectAtIndex:indexPath.row] valueForKey:@"Image"];

UIImage *img = [[UIImage alloc] initWithData:tempData];

并且我遇到了这个错误:

由于未捕获的异常 'NSInvalidArgumentException',应用程序终止,原因是:'-[__NSCFString bytes]:向实例0x5f8c000发送无法识别的选择器'

我做错了什么?


如何在SQLite中存储NSData? - Hector
3
为什么你需要对 tempData 进行 alloc/init 后立即覆写它? - Sergey Kalinichenko
你的数据肯定有问题!!! - Nikhil Bansal
@Hector:我使用这行代码存储我的数据 app.picdata = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; - kb920
2个回答

1
你可以这样做。
NSData *data = yourData;
UIImage *image = [UIImage imageWithData:data];

或者

NSData *data = yourData;
UIImage *image = [[UIImage alloc] initWithData:data];
[image release];

如果它不起作用,那么可能是您的NSData值存在问题。


1

将图像插入SQLite数据库:

sqlite3_bind_blob(compiledStatement,i, [image_data bytes], [image_data length], SQLITE_TRANSIENT);

从SQLite获取图像:

NSData *dataForCachedImage = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, i) length: sqlite3_column_bytes(compiledStatement, i)];           
UIImage *img = [UIImage imageWithData:dataForCachedImage];

这里的“i”是您的数据库列号。


好的,但是"compiledStatement"的意思是dbpath还是其他什么!! - kb920
sqlite3_stmt *compiledStatement; - Hector
@keyurbhalodiya 请参考此链接:https://dev59.com/FlTTa4cB1Zd3GeqPsXKv - Hector

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接