将图像存储到 sqlite 数据库中 [英] Store images in to sqlite database

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

问题描述

我知道很多人都问过这个问题,关于为什么使用 sqlite、使用 FMDB、核心数据"等有几个讨论和争论.在数据库中存储或插入图像.但请理解我的问题我非常习惯于 sqlite3,我无法使用核心数据或其他一些数据库.而且我正处于 iphone 技术的学习阶段.所以我请求提供一个只能处理 sqlite 的解决方案;)

I know this question was asked by many people and there are several discussions and arguments on "Why to use sqlite,use FMDB,core data" etc.. to store or insert images in database.But please do understand my problem that I am very much used to sqlite3 and I am unable to go with core data or some other db.Also I am just in learning stages of iphone technology.So I request to please provide me a solution that can be dealt with sqlite only ;)

如果我的请求有任何错误,我们深表歉意!

Apologies if any wrong in my request!

我有一个文本字段,它会在输入字母时显示带有建议的表格,即来自联系人,比如我输入字母L",Lakshaman Rao,Prasanna Lakshmi",Lokesh Sharan"等.

I have a text field that will show table with suggestions when a letter is typed,i.e. from contacts,say if I type letter "L" ,"Lakshaman Rao,Prasanna Lakshmi,"Lokesh Sharan" etc..

现在我正在尝试从各个联系人中获取相应的联系人图片.所以我搜索了有关如何检索联系人图片的代码示例并实现了以下方式:

Now I am trying to get the corresponding contact picture from the respective contact.So I have searched for code sample on how to retrieve contact images and implemented the following way:

NSData *imgData = nil;
imgData = (NSData *)ABPersonCopyImageData(ref);
contactImage = [UIImage imageWithData:imgData]; 

现在我搜索了类似的问题 这里那里和常见的答案是将图像保存在应用程序的文档目录中,并在数据库中仅保存图像的路径.

Now I have searched for similar kind of questions here and there and the common answer was to save the image in documents directory of the app and in the db save only the path of the image.

因为在 db 中插入 blob 会使我们的 db 非常慢:(

As inserting in blobs in db will make our db very very slow :(

但是我该怎么做.我无法正确理解链接中的代码片段,如何将 UIImage 类型的 contactImage 转换为 NSString 以便我可以将字符串插入数据库以 VARCHAR 类型保存记录的表!

But how do I do this.I am unable to understand the code snippet in the links I have gone through properly,how do I convert the contactImage which is of UIImage type to NSString so that,I can insert the string to database table holding a record as VARCHAR type!

编辑

这是那里建议的代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];

这是我实现的代码:

ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

 NSString *contactFirstName = nil;
 NSString *contactLastName = nil;
 NSString *fullName = nil;

    for ( int i = 0; i < nPeople; i++ )
    {
        ref = CFArrayGetValueAtIndex( allPeople, i );
        contactFirstName = [[[NSString alloc] initWithString:(NSString *)ABRecordCopyValue(ref, kABPersonFirstNameProperty)]autorelease];
        contactLastName = [[[NSString alloc] initWithString:(NSString *)ABRecordCopyValue(ref, kABPersonLastNameProperty)]autorelease];

        contactLastName = [NSString stringWithFormat:@" %@",contactLastName];
        fullName = [contactFirstName stringByAppendingString:contactLastName];

        [contactList addObject:fullName];
}

    NSData *imgData = nil;
    imgData = (NSData *)ABPersonCopyImageData(ref);
    contactImage = [UIImage imageWithData:imgData];  

    CFRelease(allPeople);
    CFRelease(addressBook);

请帮帮我,在如何处理和继续处理这个问题上苦苦挣扎:(

Please help me out,Struggling very badly on how to deal and move on with this :(

提前谢谢大家:)

推荐答案

用于保存图片到文档

- (void)saveImage:(UIImage *)image forPerson:(NSString *)fullName  {
    //  Make file name first
    NSString *filename = [fullName stringByAppendingString:@".png"]; // or .jpg

    //  Get the path of the app documents directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    //  Append the filename and get the full image path
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:filename];

    //  Now convert the image to PNG/JPEG and write it to the image path
    NSData *imageData = UIImagePNGRepresentation(image);
    [imageData writeToFile:savedImagePath atomically:NO];   

    //  Here you save the savedImagePath to your DB
    ...
}

所以,你以后可以通过

- (UIImage *)loadImage:(NSString *)filePath  {
    return [UIImage imageWithContentsOfFile:filePath];
}

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

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