zoukankan      html  css  js  c++  java
  • 图像存取

    //
    //  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
  • 相关阅读:
    getAttribute()与getParameter的区别
    【转】request和response的页面跳转
    UUIDUtils工具类
    QueryRunner类常用的方法
    HttpServletResponse,HttpServletRequest详解
    jquery简洁遮罩插件
    ngnix 参考配置
    apt-get卡住0 [连接到archive.ubuntu.com]
    Ubuntu 16.04安装SQLite Browser操作SQLite数据库
    IDEA 去掉 ALT + / 自动补全
  • 原文地址:https://www.cnblogs.com/lidongq/p/3943383.html
Copyright © 2011-2022 走看看