//将所需要的FMDB和生成二维码图片的SDK加入到工程中
//创建一个继承于NSObject的类:DataBase
//创建一个继承于NSObject的类:Picture
//创建一个ShowviewControllers继承于UIviewcontroller
//加入libsqlite3.tbd框架
DataBase.h-------------------
#import "FMDatabase.h"
#import "Picture.h"
@interface DataBase : NSObject
//单例
+(DataBase *)ShareFM;
//添加
-(void)addName:(Picture *)picture;
//查询
-(NSMutableArray *)getAll;
@end
DataBase.m-------------
#import "FMResultSet.h"
//静态
static FMDatabase *fmdb;
static DataBase *db;
@implementation DataBase
+(DataBase *)ShareFM{
if (!db) {
db=[[DataBase alloc] init];
[db initDB];
}
return db;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
if (!db) {
db=[super allocWithZone:zone];
}
return db;
}
-(id)copy{
return self;
}
-(id)mutableCopy{
return self;
}
-(void)initDB{
//沙盒路径
NSString *documentPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[documentPath stringByAppendingPathComponent:@"ll.sqlite"];
NSLog(@"%@",path);
//初始化fmdb
fmdb=[[FMDatabase alloc] initWithPath:path];
//创建数据列表
if ([fmdb open]) {
[fmdb executeUpdate:@"CREATE TABLE heng(ids INTEGER PRIMARY KEY AUTOINCREMENT ,name TEXT)"];
[fmdb close];
}else{
NSLog(@"数据表创建失败!");
}
}
//添加
-(void)addName:(Picture *)picture{
//正则表达式
if (picture.name.length<7) {
NSLog(@"不符合!");
}else{
[fmdb open];
BOOL bb=[fmdb executeUpdate:@"insert into heng VALUES(null,?)",picture.name];
if (bb) {
NSLog(@"添加成功!");
}else{
NSLog(@"添加失败!");
}
[fmdb close];
}
}
//查询
-(NSMutableArray *)getAll{
//初始化一个可变数组
NSMutableArray *arr=[[NSMutableArray alloc] init];
[fmdb open];
//初始化结果合集
FMResultSet *fmset=[[FMResultSet alloc] init];
fmset=[fmdb executeQuery:@"select * from heng"];
while([fmset next]) {
NSInteger IDs=[fmset intForColumn:@"ids"];
NSString *name=[fmset stringForColumn:@"name"];
Picture *p=[[Picture alloc] init];
p.IDs=IDs;
p.name=name;
[arr addObject:p];
}
[fmdb close];
return arr;
}
picTure-------------
@property(nonatomic ,assign)NSInteger IDs;
@property(nonatomic , strong)NSString *name;
viewController.m--------------
#import "QRCodeGenerator.h"
#import "Picture.h"
#import "DataBase.h"
#import "ShowViewController.h"
@property(nonatomic , strong)UITextField *tf;
@property(nonatomic , strong)UIImageView *iv;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor=[UIColor whiteColor];
UIBarButtonItem *rItem=[[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleDone target:self action:@selector(item)];
self.navigationItem.rightBarButtonItem=rItem;
self.tf=[[UITextField alloc] initWithFrame:CGRectMake(80, 150, 150, 30)];
self.tf.layer.borderWidth=0.5;
[self.view addSubview:self.tf];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame=CGRectMake(250, 150, 50, 30);
btn.backgroundColor=[UIColor orangeColor];
[btn setTitle:@"生成" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
self.iv=[[UIImageView alloc] initWithFrame:CGRectMake(100, 250, 200, 200)];
// self.iv.backgroundColor=[UIColor grayColor];
[self.view addSubview:self.iv];
}
-(void)item{
ShowViewController *show=[[ShowViewController alloc] init];
[self.navigationController pushViewController:show animated:YES];
}
-(void)add{
//生成二维码图片
UIImage *image=[QRCodeGenerator qrImageForString:self.tf.text imageSize:self.iv.frame.size.width];
self.iv.image=image;
//在沙河路径中生成图片文件
NSString *filepath=[NSString stringWithFormat:@"%@/%@.png",NSHomeDirectory(),self.tf.text];
//写入文件
[UIImagePNGRepresentation(image) writeToFile:filepath atomically:YES];
NSLog(@"==%@",filepath);
Picture * p=[[Picture alloc] init];
p.name=self.tf.text;
[[DataBase ShareFM] addName:p];
NSLog(@"%ld",p.IDs);
}
showViewController.m-------------------
#import "Picture.h"
#import "DataBase.h"
//签订表格协议
@interface ShowViewController ()<UITableViewDataSource,UITableViewDelegate>{
UITableView *table;
NSMutableArray *arr;
}
@end
@implementation ShowViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//初始化表格
table=[[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
table.delegate=self;
table.dataSource=self;
[self.view addSubview:table];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
arr=[[DataBase ShareFM] getAll];
NSLog(@"%@",arr);
[table reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//单元格id
static NSString *cellid=@"cellid";
//复用池中查找
UITableViewCell *cell=[table dequeueReusableCellWithIdentifier:cellid];
if (!cell) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
}
Picture *p=arr[indexPath.row];
cell.textLabel.text=p.name;
NSString *filepath=[NSString stringWithFormat:@"%@/%@.png",NSHomeDirectory(),p.name];
UIImage *ima=[UIImage imageWithContentsOfFile:filepath];
cell.imageView.image=ima;
return cell;
}