簡體   English   中英

Objective-C從SQLite讀取圖像

[英]Objective-c read image from sqlite

    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 (Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString bytes]: unrecognized selector sent to instance 0x1e5c46d0')

SQLiteAccess.h - -容易獲得sqlite的PS類http://pastebin.com/BFxFry7T - SQLiteAccess.m - http://pastebin.com/m67yNVLm

是的,我認為問題出在這里:

[NSString stringWithFormat:@"insert into images (photo) values ('%@')", imageData]]; // i use class SQLiteaccess

直接使用SQLite時,應通過sqlite3_bind_blob()設置二進制數據,但實際上是將二進制數據設置為字符串。

因此,當您取回數據時,它不是二進制數據,而是String對象,當然,字符串對象不響應-[NSData bytes]


同時,我認為您應該檢查SQLite DB文件中的字段類型。 photo實際上設置為blob嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM