我正在使用 sqlite db 来保存文档目录的图像路径。这是我的代码:
NSData * imageData = UIImageJPEGRepresentation(image, 0.8);
NSLog(@"Image data length== %d",imageData.length);
if (imageData != nil)
{
UIImage *resizedImg = [self scaleImage:[UIImage imageWithData:imageData] toSize:CGSizeMake(150.0f,150.0f)];
NSData * imageData = UIImageJPEGRepresentation(resizedImg, 0.2);
NSLog(@"*** Image data length after compression== %d",imageData.length);
NSString *nameofimg=[NSString stringWithFormat:@"%@",resizedImg];
NSString *substring=[nameofimg substringFromIndex:12];
NSString *new=[substring substringToIndex:7];// Get image name
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentdirectory=[path objectAtIndex:0];
NSString *newFilePath = [NSString stringWithFormat:[documentdirectory stringByAppendingPathComponent: @"/%@.png"],new];
[imageData writeToFile:newFilePath atomically:YES];
imgpath=[[NSString alloc]initWithString:newFilePath];
NSLog(@"doc img in img method === %@",imgpath);
}
databasepath=[app getDBPath]; // i have this method in delegate
if (sqlite3_open([databasepath UTF8String], &dbAssessor) == SQLITE_OK)
{
NSString *selectSql = [NSString stringWithFormat:@"insert into imagetb(imagename,image) VALUES(\"%@\",\"%@\") ;",yourImgName,imgpath];
NSLog(@"Query : %@",selectSql);
const char *sqlStatement = [selectSql UTF8String];
sqlite3_stmt *query_stmt;
sqlite3_prepare(dbAssessor, sqlStatement, -1, &query_stmt, NULL);
if(sqlite3_step(query_stmt)== SQLITE_DONE)
{
NSLog(@"home image updated. :)");
app.houseImage=imgpath;
}
else
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Sorry" message:@"Failed To Save Home Image." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
sqlite3_finalize(query_stmt);
}
sqlite3_close(dbAssessor);
这些代码将图像保存在 sqlite 中的文档目录和路径中。现在我想将文档目录中的图像显示到 imageview。怎么做?任何想法?