在iphone中从Sqllite数据库中获取图像 [英] Fetch Image From Sqllite Database in iphone

查看:43
本文介绍了在iphone中从Sqllite数据库中获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像保存数据库并在数据库中获取图像.我在NSData中转换图像,图像是成功存储在数据库中,但是当我从数据库中获取该图像时,它会崩溃.它发出此警告

I Want to save the image in Database and fetch the image in database.i am convert the image in NSData and the image is store successfully in database but when i fetch that image from database then it crash.it give this warning

警告:- -[__NSCFString bytes]:无法识别的选择器发送到实例 0x7916000

下面的代码用于将图像保存在数据库中

NSData *dataImage1 = UIImagePNGRepresentation(image3.image);
 NSString   *str = [NSString stringWithFormat:@"Insert into Gallery(ImageName) VALUES ('%@')",dataImage1];
[Database executeQuery:str];

//Database.m

+(NSString* )getDatabasePath{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Database"];
    return writableDBPath;

}

+(NSMutableArray *)executeQuery:(NSString*)str{

    sqlite3_stmt *statement= nil;
    sqlite3 *database;
    NSString *strPath = [self getDatabasePath];
    NSMutableArray *allDataArray = [[NSMutableArray alloc] init];
    if (sqlite3_open([strPath UTF8String],&database) == SQLITE_OK) {
        if (sqlite3_prepare_v2(database, [str UTF8String], -1, &statement, NULL) == SQLITE_OK) {


            while (sqlite3_step(statement) == SQLITE_ROW) {
                            NSInteger i = 0;
                NSInteger iColumnCount = sqlite3_column_count(statement);
                NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
                while (i< iColumnCount) {
                    NSString *str = [self encodedString:(const unsigned char*)sqlite3_column_text(statement, i)];


                NSString *strFieldName = [self encodedString:(const unsigned char*)sqlite3_column_name(statement, i)];

                    [dict setObject:str forKey:strFieldName];
                    i++;
                }

                [allDataArray addObject:dict];
                [dict release];
            }
        }

        sqlite3_finalize(statement);
    } 
    sqlite3_close(database);
    return allDataArray;
}

//获取图片

NSString *str =[NSString stringWithFormat:@"Select * from Gallery where id = 1"];
NSMutableArray *arra =[Database executeQuery:str];
NSData *d =[[arra objectAtIndex:0] valueForKey:@"ImageName"];
UIImage *img = [[UIImage alloc] initWithData:d];
NSLog(@"%@",img);

提前致谢

推荐答案

您的问题是您无法通过构建带有 stringWithFormat 的 SQL 语句来插入二进制数据.您将NSData(二进制数据)插入到名为ImageName 的字段中,并且您不能使用stringWithFormat 来构建该SQL 语句.

Your problem is that you cannot insert binary data by building a SQL statement with a stringWithFormat. You're inserting the NSData (binary data) into a field called ImageName, and you cannot use stringWithFormat to build that SQL statement.

您真的想插入图像本身吗?在这种情况下,您将使用如下所示的 SQL,使用 ? 占位符,然后使用 sqlite3_bind_blob 在您调用 sqlite3_step 之前使用您的 NSData 获取列号 1.因此,SQL 看起来像:

Did you really mean to insert the image itself? In that case you'd use SQL like below, with a ? placeholder, and then use sqlite3_bind_blob for column number 1 with your NSData before you call sqlite3_step. Thus, the SQL would look like:

Insert into Gallery(ImageName) VALUES (?)

(顺便说一句,我不喜欢为图像的实际数据选择 ImageName.我要么使用 Image 作为实际图像数据,或 ImageName 为图像的名称.)

(As an aside, I'm not a fan of the choice of ImageName for the actual data of the image. I'd either use Image for the actual image data, or ImageName for the name of the image.)

然后,在准备好 SQL 之后,您将调用:

And then, after preparing your SQL, you'd then call:

sqlite3_bind_blob(statement, 1, [dataImage1 bytes], [dataImage1 length], SQLITE_TRANSIENT);

然后您可以像平常一样调用 sqlite3_step 来执行 SQL 以插入数据.

You can then invoke sqlite3_step, like normal, to execute the SQL to insert the data.

检索数据时,在您成功sqlite3_step后,您将使用适当的sqlite3_column_blob函数:

When retrieving the data, after you successfully sqlite3_step, you'd use the appropriate sqlite3_column_blob functions:

const void *bytes = sqlite3_column_blob(statement, 0);
int len = sqlite3_column_bytes(statement, 0);
NSData *data = [NSData dataWithBytes:bytes length:len];
UIImage *image = [UIImage imageWithData:data];

话虽如此,SQLite 在存储大型 blob 数据方面是出了名的低效.您可能希望将图像存储在 Documents 文件夹中,然后将文件名存储在 ImageName 字段中.

Having said that, SQLite is notoriously inefficient in storing large blob data. You might want to store the image in the Documents folder and then just store the filename in your ImageName field.

这篇关于在iphone中从Sqllite数据库中获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆