我在项目中使用sqlite 以前我已将图像保存在数据中,但现在我正在保存图像,如下面的代码
NSUInteger len = [entObject.photoImageData length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [entObject.photoImageData bytes], len);
insertString = [NSString stringWithFormat:@"INSERT INTO TBL_COUNTDOWN (DAIRYID,EVENTID,DESCRIPTION,EVENTIMAGE) VALUES (\"%@\",\"%d\",\"No Data\",\"%s\")",self.dairyId,self.eventId,byteData];
对于检索图像,我使用以下代码
NSData *dataForCachedImage = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 3) length: sqlite3_column_bytes(statement, 3)];
NSLog(@"dataForCachedImage/getAllEvents is %@",dataForCachedImage);
UIImage *cachedImage = [UIImage imageWithData:dataForCachedImage];
NSLog(@"cachedImage/getAllEvents is %@",cachedImage);
对于NSLog中的dataForCachedImage我得到数据但是对于cachedImage我得到了空
我的代码中没有错。请帮帮我
提前致谢
答案 0 :(得分:1)
您可以将图像保存到文档目录并将其路径存储到sqlite数据库 保存图像代码如下
#define DOCUMENTS_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
// Create a new dated file
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
NSString *caldate = [now description];
NSString *filePath= [NSString stringWithFormat:@"%@/%@.jpg", DOCUMENTS_FOLDER,caldate];
NSURL *url = [NSURL fileURLWithPath:filePath];
UIImage *image = [UIImage imageNamed:@"name.jpg"];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:filePath atomically:YES];
任何时候你想要检索Image然后得到它然后使用以下方法来getImage
- (UIImage*)getImage
{
NSString *imagePath = [DOCUMENTS_FOLDER stringByAppendingPathComponent:@"imagename.jpg"];
UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
return img;
}
删除文件
NSFileManager *fileManager=[NSFileManager defaultManager];
[fileManager removeItemAtPath:savedFilePath error:nil];
这可能会对你有所帮助..