이미지 액세스

10260 단어 이미지
//

//  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

좋은 웹페이지 즐겨찾기