首页 > 数据库技术 > 详细

使用FMDB,libqrencode实现二维码的生成并且保存到数据库

时间:2015-12-23 20:58:01      阅读:253      评论:0      收藏:0      [点我收藏+]
/**
 * 1.首先导入第三方库FMDB和libqrencode,添加库libsqlite3.tbd,禁用arc:FMDatabasePool类和FMDatabaseQueue类
 */
  
 /**
 * 2.在AppDelegate.m中设置根视图控制器
 */
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ViewController *vc = [[ViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    self.window.rootViewController = nav;
    return YES;
}
 
/**
 * 3.创建模型类CodeModel,继承自NSObject
 *   定义模型类的两个属性id和name
 */
@interface CodeModel : NSObject
@property (nonatomic,strong)NSString *codeName;
@property (nonatomic,assign)int codeId;
@end
 
 
/**
 * 4.创建业务处理类CodeHandle,继承自NSObject
 *   导入数据库头文件FMDatabase.h和FMResultSet.h,模型类头文件CodeModel.h
 *   在.h中声明单例方法,增加数据的方法和查找数据的方法
 */
+(instancetype)sharedHandle;
-(void)insertCode:(CodeModel *)codename;
-(NSMutableArray*)getAll;
 
/**
 * 5.在CodeHandle.m中实现单例类方法
 */    
#import "CodeHandle.h"
static FMDatabase *fmdb;
static CodeHandle *_codeHandle;
@implementation CodeHandle
 
+(instancetype)sharedHandle
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _codeHandle = [[CodeHandle alloc]init];
        [_codeHandle initDB];
    });
    return _codeHandle;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    if (!_codeHandle) {
        _codeHandle = [super allocWithZone:zone];
    }
    return _codeHandle;
}
 
-(void)initDB
{
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path = [docPath stringByAppendingPathComponent:@"chenrui.sqlite"] ;
    NSLog(@"path = %@",path);
     
    fmdb = [[FMDatabase alloc]initWithPath:path];
    if ([fmdb open]) {
        [fmdb executeUpdate:@"CREATE TABLE CODE(codeId INTEGER PRIMARY KEY AUTOINCREMENT,codeName TEXT)"];
        [fmdb close];
    }
    else
        NSLog(@"创建数据表失败!");
     
}
-(void)insertCode:(CodeModel *)codename
{
    if (codename.codeName.length<7) {
        NSLog(@"不符合");
    }
    else
    {
        [fmdb open];
        BOOL insertSql = [fmdb executeUpdate:@"INSERT INTO CODE VALUES(null,?)",codename.codeName];
        if (insertSql) {
            NSLog(@"添加成功");
        }
        NSLog(@"添加失败");
        [fmdb close];
    }
}
-(NSMutableArray *)getAll
{
    NSMutableArray *arr = [NSMutableArray array];
    [fmdb open];
    FMResultSet *fmset = [[FMResultSet alloc]init];
    fmset = [fmdb executeQuery:@"SELECT *FROM CODE"];
    while ([fmset next]) {
        int codeId = [fmset intForColumn:@"codeId"] ;
        NSString *codeName = [fmset stringForColumn:@"codeName"];
        CodeModel *cm = [[CodeModel alloc]init];
        cm.codeId = codeId;
        cm.codeName = codeName;
        [arr addObject:cm];
    }
    return arr;
}
 
/**
 * 6.在控制器类中实现生成二维码和跳转到下一界面
 *   导入生成二维码的头文件QRCodeGenerator.h,
 *   模型类头文件CodeModel.h,业务处理类头文件CodeHandle.h,下一界面头文件ShowResultTableViewController.h
 */
 
#import "ViewController.h"
 
@interface ViewController ()
{
    UITextField *inputTF;
    UIImageView *imgView;
}
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
     
    self.view.backgroundColor = [UIColor whiteColor];
     
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"tiaozhuan" style:UIBarButtonItemStylePlain target:self action:@selector(tiaozhuan)];
    self.navigationItem.rightBarButtonItem =right;
     
    inputTF = [[UITextField alloc]initWithFrame:CGRectMake(100, 150, 200, 30)];
    inputTF.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:inputTF];
     
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(300, 150, 50, 30);
    [btn setTitle:@"生成" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(addCode) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
     
    imgView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 200, 200)];
    [self.view addSubview:imgView];
}
-(void)tiaozhuan
{
    ShowResultTableViewController *show = [[ShowResultTableViewController alloc]init];
    [self.navigationController pushViewController:show animated:YES];
}
-(void)addCode
{
    UIImage *img = [QRCodeGenerator qrImageForString:inputTF.text imageSize:200.0];
    imgView.image = img;
     
    NSString *imgPath = [NSString stringWithFormat:@"%@/%@.png",NSHomeDirectory(),inputTF.text];
    [UIImagePNGRepresentation(img) writeToFile:imgPath atomically:YES];
    NSLog(@"imgPath = %@",imgPath);
     
    CodeModel *cm = [[CodeModel alloc]init];
    cm.codeName = inputTF.text;
    [[CodeHandle sharedHandle]insertCode:cm];
    NSLog(@"codeId = %d",cm.codeId);
}
 
/**
 * 7.在下一界面设置单元格显示内容为二维码图片和输入的文本内容
 */
#import "ShowResultTableViewController.h"
#import "CodeModel.h"
#import "CodeHandle.h"
@interface ShowResultTableViewController ()
{
    NSMutableArray *arr;
}
@end
 
@implementation ShowResultTableViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];  
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return arr.count;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    arr = [[CodeHandle sharedHandle]getAll];
    NSLog(@"arr = %@",arr);
    [self.tableView reloadData];
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *str = @"sdf";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str ];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
    }
    CodeModel *cm = arr[indexPath.row];
    cell.textLabel.text = cm.codeName;
     
    NSString *imgPath = [NSString stringWithFormat:@"%@/%@.png",NSHomeDirectory(),cm.codeName];
    UIImage *img = [UIImage imageWithContentsOfFile:imgPath];
    cell.imageView.image = img;
     
    return cell;
}
 
@end

使用FMDB,libqrencode实现二维码的生成并且保存到数据库

原文:http://www.cnblogs.com/Bo-tree/p/5071011.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!