码迷,mamicode.com
首页 > 其他好文 > 详细

图像存取

时间:2014-08-29 00:00:56      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   ar   for   数据   2014   

//
//  ViewController.m
//  图片存取
//
//  Created by apple on 14-8-28.
//  Copyright (c) 2014年 apple. All rights reserved.
//

#import "ViewController.h"
#import <sqlite3.h>
@interface ViewController ()
            

@end

@implementation ViewController
            
- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *imagePath=[[NSBundle mainBundle]pathForResource:@"20140727_4a88ec45b0ba50d4f06015b18521e12b" ofType:@"png"];
    NSData *imageData=UIImagePNGRepresentation([UIImage imageWithContentsOfFile:imagePath]);
    NSString *bdPath=[NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/image.bd"];
    NSLog(@"%@",bdPath);
    
    sqlite3 *imageDatabase;
    int ret=sqlite3_open([bdPath UTF8String],&imageDatabase);
    if (ret!=SQLITE_OK) {
        NSLog(@"open failed");
        return;
    }
    
    NSString *sql=@"CREATE TABLE IF NOT EXISTS history (hid integer primary key autoincrement not null, name text,image blob)";
    sqlite3_stmt *stmt;
    ret=sqlite3_prepare(imageDatabase, [sql UTF8String], -1, &stmt, NULL);
    if (ret!=SQLITE_OK) {
        NSLog(@"prepare  failed");
        
        sqlite3_close(imageDatabase);
        return;
    }
    
    //执行SQL语句
        ret = sqlite3_step(stmt);
        if (ret != SQLITE_DONE) {
            NSLog(@"step failed!");
    
            //终止会话
            sqlite3_finalize(stmt);
            sqlite3_close(imageDatabase);
            return;
        }
    
        sqlite3_finalize(stmt);
    
    NSString *insertSQL2 = @"INSERT INTO history (name,image) VALUES(?,?)";
    sqlite3_stmt *stmt2;
    ret = sqlite3_prepare_v2(imageDatabase, [insertSQL2 UTF8String], -1, &stmt2, NULL);
    if (ret != SQLITE_OK) {
        NSLog(@"prepare stmt2 failed");
        sqlite3_close(imageDatabase);
        return;
    }
    
    NSString *name = @"赵柳";
    
    //给会话绑定数据
    sqlite3_bind_text(stmt2, 1, [name UTF8String], -1, NULL);
    sqlite3_bind_blob(stmt2, 2, [imageData bytes], [imageData length], NULL);
    ret = sqlite3_step(stmt2);
    if (ret != SQLITE_DONE) {
        NSLog(@"insert stmt2 failed");
        sqlite3_finalize(stmt2);
        sqlite3_close(imageDatabase);
        return;
    }
    else {
        int count = sqlite3_changes(imageDatabase);
        NSLog(@"insert success: %d", count);
    }
    sqlite3_close(imageDatabase);
    
    //取图片
    NSString *select = @"SELECT * FROM history where name=?";
    sqlite3_stmt *stmtGet;
    ret = sqlite3_prepare_v2(imageDatabase, [select UTF8String], -1, &stmtGet, NULL);
    if (ret != SQLITE_OK) {
        NSLog(@"prepare stmt2 failed");
        sqlite3_close(imageDatabase);
        return;
    }
    else
    {
        NSString *name=@"赵柳";
        sqlite3_bind_text(stmtGet, 1,[name UTF8String], -1, NULL);
        if (sqlite3_step(stmtGet)==SQLITE_ROW) {
            int byte=sqlite3_column_bytes(stmtGet, 2);
            Byte *imgByte=(Byte *)sqlite3_column_blob(stmtGet, 2);
            NSData *imgData=[NSData dataWithBytes:imgByte length:byte];
            UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
            imageView.backgroundColor=[UIColor redColor];
            imageView.image=[UIImage imageWithData:imgData];
            [self.view addSubview:imageView];
        }
        sqlite3_close(imageDatabase);
    }
    
//    NSString *name = @"赵柳";
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

图像存取

标签:style   blog   color   os   io   ar   for   数据   2014   

原文地址:http://www.cnblogs.com/lidongq/p/3943383.html

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