IOS FMDB-insert and read UIImage
1: Import FMDB to save the image
# Import BaseViewController. h
# Import FMDatabase. h
# Import FMDatabaseQueue. h
# Import PPCamaraUtil. h
# Import SecondViewController. h
Static const NSString * kIdentifier = @ BaseViewController;
Static const NSString * kStoryboardName = @ BaseViewController;
@ Interface BaseViewController ()
{
FMDatabase * _ db;
}
@ Property (strong, nonatomic) IBOutlet UIButton * postImage;
@ End
@ Implementation BaseViewController
+ (Id) createViewController :( id) createArgs {
UIStoryboard * storyboard = [UIStoryboard storyboardWithName :( NSString *) kStoryboardName bundle: nil];
BaseViewController * vc = [storyboard instantiateViewControllerWithIdentifier :( NSString *) kIdentifier];
Return vc;
}
-(Void) viewDidLoad {
[Super viewDidLoad];
[Self openDB]; // open the database
[_ PostImage addTarget: self action: @ selector (postImageFromPhoto) forControlEvents: UIControlEventTouchUpInside];
UIBarButtonItem * btn = [[UIBarButtonItem alloc] initWithTitle: @ get style: UIBarButtonItemStylePlain target: self action: @ selector (push)];
Self. navigationItem. rightBarButtonItem = btn;
}
-(Void) push {
SecondViewController * vc = [SecondViewController createViewController: nil];
[Self. navigationController pushViewController: vc animated: YES];
}
# Pragma mark-select from album
-(Void) postImageFromPhoto {
// PPCamaraUtil * oo = [PPCamaraUtil getInstance];
// [Oo openCamara: self uploadHeaderDelegate: self];
PPCamaraUtil * oo = [PPCamaraUtil getInstance];
[Oo openPhoto: self uploadHeaderDelegate: self];
}
# Pragma mark-Save the selected UIImage to the database
-(Void) resultImage :( UIImage *) image imageData :( NSData *) imageData filePath :( NSString *) filePath fileName :( NSString *) fileName {
NSData * data = UIImagePNGRepresentation (image );
BOOL res = [_ db open];
If (! Res ){
NSLog (@ failed to open );
}
Res = [_ db executeUpdate: @ insert into USER (image) values (?), Data];
If (! Res ){
NSLog (@ database insertion failed );
}
[_ Db close];
}
-(Void) openDB {
_ Db = [FMDatabase databaseWithPath: [self databasePath];
// Multi-threaded database operations
// FMDatabaseQueue * queue = [FMDatabaseQueue databaseQueueWithPath: [self databasePath];
// Dispatch_queue_t q1 = dispatch_queue_create (queue1, NULL );
// Dispatch_queue_t q2 = dispatch_queue_create (queue2, NULL );
// Dispatch_async (q1, ^ {
// For (int I = 0; I <50; ++ I ){
// [Queue inDatabase: ^ (FMDatabase * db2 ){
//
//}];
//}
//});
//
// Dispatch_async (q2, ^ {
// For (int I = 0; I <50; ++ I ){
// [Queue inDatabase: ^ (FMDatabase * db2 ){
//
//}];
//}
//});
If (! [_ Db open]) {
NSLog (@ database opening failed );
Return;
} Else {
NSString * SQL = @ create table if not exists USER (id integer primary key autoincrement, name, score, image );
BOOL success = [_ db executeUpdate: SQL];
If (! Success ){
NSLog (@ table creation failed );
} Else {
NSLog (@ create table succeed );
}
[_ Db close];
}
}
// Database path
-(NSString *) databasePath {
NSString * path = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
NSString * dbPath = [path stringByAppendingPathComponent: @ user. db];
Return dbPath;
}
-(Void) didReceiveMemoryWarning {
[Super didReceiveMemoryWarning];
}
@ End
2: Read and display images from the database
# Import SecondViewController. h
# Import FMDatabase. h
Static const NSString * kIdentifier = @ SecondViewController;
Static const NSString * kStoryboardName = @ SecondViewController;
@ Interface SecondViewController ()
{
FMDatabase * _ db;
}
@ Property (strong, nonatomic) IBOutlet UITableView * tableView;
@ Property (strong, nonatomic) NSMutableArray * cellInfoArray;
@ End
@ Implementation SecondViewController
+ (Id) createViewController :( id) createArgs {
UIStoryboard * storyboard = [UIStoryboard storyboardWithName :( NSString *) kStoryboardName bundle: nil];
SecondViewController * vc = [storyboard instantiateViewControllerWithIdentifier :( NSString *) kIdentifier];
Return vc;
}
-(Void) viewDidLoad {
[Super viewDidLoad];
[Self initTableView];
[Self openDB];
}
-(Void) openDB {
_ Db = [FMDatabase databaseWithPath: [self databasePath];
If (! [_ Db open]) {
NSLog (@ database opening failed );
Return;
} Else {
Self. cellInfoArray = [[NSMutableArray alloc] init];
// Note: The FMResultSet can be disabled because it is automatically disabled when the database is disabled.
FMResultSet * resultSet = [_ db executeQuery: @ select * from user];
While ([resultSet next]) {
NSData * data = [resultSet dataForColumn: @ image];
[Self. cellInfoArray addObject: data];
}
}
}
-(NSString *) databasePath {
NSString * path = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
NSString * dbPath = [path stringByAppendingPathComponent: @ user. db];
Return dbPath;
}
-(Void) initTableView {
Self. tableView. dataSource = self;
Self. tableView. delegate = self;
}
-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {
Return 100;
}
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {
Return 1;
}
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
Return self. cellInfoArray. count;
}
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: @ cellID];
If (cell = nil ){
Cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @ cellID];
}
NSData * data = self. cellInfoArray [indexPath. row];
UIImage * image = [UIImage imageWithData: data];
Cell. imageView. image = image;
Return cell;
}
-(Void) didReceiveMemoryWarning {
[Super didReceiveMemoryWarning];
}
@ End