Insertion and reading of IOS Fmdb--uiimage

Source: Internet
Author: User


1: First pour fmdb save picture

#import "BaseViewController.h"
#import "FMDatabase.h"
#import "FMDatabaseQueue.h"
#import "PPCamaraUtil.h"
#import "SecondViewController.h"

static const NSString *[email protected] "Baseviewcontroller";
static const NSString *[email protected] "Baseviewcontroller";
@interface Baseviewcontroller () <pppickerimagedelegate,uinavigationcontrollerdelegate, Uiimagepickercontrollerdelegate>
{
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 database
[_postimage addtarget:self Action: @selector (Postimagefromphoto) forcontrolevents:uicontroleventtouchupinside];
Uibarbuttonitem *btn=[[uibarbuttonitem alloc]initwithtitle:@ "Take" 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-Choose from albums
-(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 (@ "Open failed");
}
res=[_db executeupdate:@ "INSERT into USER (image) VALUES (?)", data];
if (!res) {
NSLog (@ "Insert database Failed");
}
[_db close];

}
-(void) opendb{
_db=[fmdatabase databasewithpath:[self DatabasePath]];
Multi-threaded Operations database
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 < ++i) {
[Queue indatabase:^ (Fmdatabase *db2) {
//
//            }];
//        }
//    });
//
Dispatch_async (Q2, ^{
for (int i = 0; i < ++i) {
[Queue indatabase:^ (Fmdatabase *db2) {
//
//            }];
//        }
//    });
if (![ _DB Open]) {
NSLog (@ "Database open failed");
Return
}else{
NSString *[email protected] "CREATE table if not exists USER (ID integer primary key autoincrement,name,score,image)";
BOOL success = [_db executeupdate:sql];
if (!success) {
NSLog (@ "Failed to create TABLE");
}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 the picture from the database and display


#import "SecondViewController.h"
#import "FMDatabase.h"

static const NSString *[email protected] "Secondviewcontroller";
static const NSString *[email protected] "Secondviewcontroller";
@interface Secondviewcontroller () <UITableViewDataSource,UITableViewDelegate>
{
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 open failed");
Return
}else{
Self.cellinfoarray = [[Nsmutablearray alloc]init];
Note that the fmresultset here can not be closed because Fmresultset automatically shuts down when the database is closed
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

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Insertion and reading of IOS Fmdb--uiimage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.