iOS FMDB--UIImage的插入與讀取

來源:互聯網
上載者:User

iOS FMDB--UIImage的插入與讀取

 

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 ()
{
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 ()
{
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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.