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

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

问题描述

我知道这个问题是由许多人提出的,并有几个讨论和争论,为什么使用sqlite,使用FMDB,核心数据等..存储或插入图像在数据库。但请理解我的问题我非常习惯于sqlite3,我无法去与核心数据或一些其他db.Also我只是在iphone技术的学习阶段。我请求请提供一个解决方案,只能处理sqlite;)



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



我有一个文本字段,一个字母被打字,即从联系人,说如果我键入字母L,Lakshaman饶,Prasanna Lakshmi,Lokesh Sharan等。



现在我想获得相应的联系人我已经搜索了如何检索联系人图片的代码示例,并实现了以下方式:

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

现在我搜索了类似的问题此处 there ,常见的答案是将图片保存在应用程序的文档目录中,并且在db中只保存图片的路径。



在db中的blob会使我们的db非常慢:(



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



EDIT



这是建议的代码:

  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 {
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);

请帮助我,对于如何处理非常困难,感谢所有人提前:)

解决方案

将图像保存到文档

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

//获取应用程序文档目录的路径
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString * documentsDirectory = [paths objectAtIndex:0];

//附加文件名并获取完整的图像路径
NSString * savedImagePath = [documentsDirectory stringByAppendingPathComponent:filename];

//现在将图像转换为PNG / JPEG并将其写入图像路径
NSData * imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];

//这里保存savedImagePath到您的DB
...
}

所以,你以后可以通过

获取图片

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


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!

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.

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

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!

EDIT

This is the code suggested there:

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

This is code I implemented:

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 :(

Thanks all in advance :)

解决方案

For saving image to documents

- (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
    ...
}

So, you later can get the image by

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

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

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