NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"image.sqlite"];
if (sqlite3_open_v2([path UTF8String], &database, SQLITE_OPEN_READWRITE, NULL) == SQLITE_OK)
{
if(selectStmt == nil)
{
const char *sql = "insert into imagetb(imagename,image) Values(?,?)";
if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) != SQLITE_OK)
// NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
NSLog(@"%s",sqlite3_errmsg(database));
}
sqlite3_bind_text(selectStmt, 1, [@"test.png" UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_blob(selectStmt, 2, [data1 bytes], [data1 length], SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(selectStmt))
NSLog(@"get: %s",sqlite3_errmsg(database));
else
NSLog(@"scan data added");
}
//showimage
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{
const char *sql = "select * from imagetb";// LIMIT 0,100 ";// where status = 1 ";
sqlite3_stmt *selectstatment;
if (sqlite3_prepare_v2(database, sql, -1, &selectstatment, NULL) == SQLITE_OK)
{
while (sqlite3_step(selectstatment) == SQLITE_ROW)
{
NSString *getorgstrid = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstatment, 0)];
NSLog(@"id->%@",getorgstrid);
[getid addObject:getorgstrid];
NSString *getorgstrname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstatment, 1)];
NSLog(@"name->%@",getorgstrname);
[getname addObject:getorgstrname];
NSData *getorgstrimage = [[NSData alloc] initWithBytes:sqlite3_column_blob(selectstatment,2)length:sqlite3_column_bytes(selectstatment, 2)];
[getimage addObject:getorgstrimage];
}
}
sqlite3_finalize(selectstatment);
}
//[tblorgland reloadData];
UIImage *image1 = [UIImage imageWithData:[getimage lastObject]];
NSLog(@"image%@",image1);
[getImageview setImage:image1];
[pool release];
==============================================================================
- (NSString *) getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"image.sqlite"];
}
- (void)viewDidLoad
{
[super viewDidLoad];
getid=[[NSMutableArray alloc]init];
getname=[[NSMutableArray alloc]init];
getimage=[[NSMutableArray alloc]init];
[self downloadimage];
[self makeDBCopyAsNeeded];
}
-(void)downloadimage
{
NSLog(@"Downloading...");
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://0.tqn.com/d/graphicssoft/1/7/g/A/5/psptubezdotcom_003.png"]]];
NSLog(@"%f,%f",image.size.width,image.size.height);
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"%@",docDir);
NSLog(@"saving png");
pngFilePath = [NSString stringWithFormat:@"%@/test.png",docDir];
data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
img=[[UIImage alloc]initWithData:data1];
[data1 writeToFile:pngFilePath atomically:YES];
NSLog(@"saving jpeg");
NSString *jpegFilePath = [NSString stringWithFormat:@"%@/test.jpeg",docDir];
NSLog(@"jpegFilePath:%@",jpegFilePath);
data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];
[data2 writeToFile:jpegFilePath atomically:YES];
NSLog(@"saving image done");
[image release];
}
- (void) makeDBCopyAsNeeded
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"image.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
NSString *dbPath = [self getDBPath];
NSLog(@"%@",dbPath);
if (success)
{
NSLog(@"Database Success Created");
return;
}
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"image.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success)
{
NSLog(@"Database:->%s",sqlite3_errmsg(database));
}
}
1
A
回答
0
0
只要寫在下面的代碼 - (空)imagePickerController:(*的UIImagePickerController)選擇器 didFinishPickingImage:(的UIImage *)圖像 editingInfo:(NSDictionary的*)editingInfo:如果您是從畫廊或照相機獲取的圖像否則跳過下,如果(爲imageData =零!),只需使用下面的代碼保存在sqlite的
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);
圖像代碼 個
+0
這個問題與從圖像選擇器中選取圖像有什麼關係? – rmaddy 2013-03-07 06:04:42
0
相關問題
- 1. 如何在圖庫中存儲SQLite數據庫中的圖像
- 2. 如何存儲在SQLite數據庫圖像iphone
- 3. 如何加載存儲在sqlite數據庫中的圖像?
- 4. 如何在html5 sqlite數據庫中存儲照片圖像?
- 5. 如何在iphone中的sqlite數據庫中插入圖像
- 6. 在SQLite數據庫中存儲圖像並在ios5中檢索
- 7. iPhone:如何在sqlite數據庫中存儲撇號(')
- 8. 如何在SQLite中存儲圖庫中的圖像?
- 9. 存儲和檢索圖像到iPhone的SQLite數據庫
- 10. 將圖像存儲到sqlite數據庫
- 11. 將圖像存儲到SQLite數據庫
- 12. 在SQLite數據庫中保存圖像
- 13. 存儲在SQLite數據庫中的圖像的URI
- 14. 如何在android中的sqlite數據庫中存儲和檢索圖像?
- 15. 如何在android中預定義的sqlite數據庫中存儲圖像?
- 16. 如何將從URL中檢索的圖像存儲在SQLite數據庫中?
- 17. 如何在sqlite中存儲圖像
- 18. 如何在iphone中使用核心數據將圖像存儲到sqlite?
- 19. 我應該在SQLite數據庫中存儲圖像嗎?
- 20. 在SQLite中存儲圖像
- 21. 在SQLite數據庫中存儲的bool
- 22. 在數據庫中存儲圖像
- 23. 圖像存儲在mysql數據庫中
- 24. 在mvc數據庫中存儲圖像
- 25. 在數據庫中存儲圖像
- 26. 在數據庫中存儲圖像
- 27. 在數據庫中存儲圖像
- 28. 將圖像存儲在數據庫中
- 29. 從iOS的SQLite數據庫中存儲和檢索圖像
- 30. 如何在iphone中的sqlite數據庫中存儲視頻文件?
您可以將圖像的路徑存儲到數據庫中,而存儲整個圖像 – 2013-03-07 05:07:13
只要有看SO問題:HTTP ://stackoverflow.com/questions/5039343/save-image-data-to-sqlite-database-in-iphone。希望這會幫助你。 – 2013-03-07 05:10:43
將圖像寫入文件系統要好得多。您可以在數據庫中引用文件名。 – rmaddy 2013-03-07 05:14:15