码迷,mamicode.com
首页 > 移动开发 > 详细

iOS FMDB--UIImage的插入与读取

时间:2015-07-17 22:52:22      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:ios   uiimage   sqlite   fmdb   


1:先倒入FMDB 保存图片

#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 ()<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];//打开数据库
    [_postImage addTarget:self action:@selector(postImageFromPhoto) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *btn=[[UIBarButtonItem alloc]initWithTitle:@"取" 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 - 从相册选取
- (void)postImageFromPhoto{
//    PPCamaraUtil *oo=[PPCamaraUtil getInstance];
//    [oo openCamara:self uploadHeaderDelegate:self];
    PPCamaraUtil *oo=[PPCamaraUtil getInstance];
    [oo openPhoto:self uploadHeaderDelegate:self];
}
#pragma mark -把选取的UIImage存到数据库
- (void)resultImage:(UIImage *)image imageData:(NSData *)imageData filePath:(NSString *)filePath fileName:(NSString *)fileName{
    NSData *data=UIImagePNGRepresentation(image);
    BOOL res=[_db open];
    if (!res) {
        NSLog(@"打开失败");
    }
    res=[_db executeUpdate:@"insert into USER(image) values(?)",data];
    if (!res) {
        NSLog(@"插入数据库失败");
    }
    [_db close];

}
- (void)openDB{
    _db=[FMDatabase databaseWithPath:[self databasePath]];
    //多线程操作数据库
//    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(@"数据库打开失败");
        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(@"创建表失败");
        }else{
            NSLog(@"create table succeed");
        }
        [_db close];
    }
}
// 数据库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:从数据库读取图片并显示


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

static const NSString *kIdentifier=@"SecondViewController";
static const NSString *kStoryboardName=@"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(@"数据库打开失败");
        return;
    }else{
        self.cellInfoArray = [[NSMutableArray alloc]init];
        //注意;此处的FMResultSet可以不用关闭,因为数据库关闭的时候FMResultSet自动关闭
        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

版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS FMDB--UIImage的插入与读取

标签:ios   uiimage   sqlite   fmdb   

原文地址:http://blog.csdn.net/darongzi1314/article/details/46930919

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!