我知道这个问题被很多人问及,有关“为什么要使用sqlite,使用FMDB,核心数据等等”的讨论和争论等等来存储或插入图像到数据库中。但请理解我的问题,我非常习惯sqlite3,我无法使用核心数据或其他数据库。另外我只是在学习iphone技术阶段。所以我请求提供给我一个解决方案,可以是处理sqlite只);将图像存储到sqlite数据库
道歉,如果在我的请求有任何错误!
我有一个文本字段,它会在输入字母时显示带有建议的表格,从联系人,说如果我键入字母“L”,“Lakshaman饶,Prasanna Lakshmi,”Lokesh Sharan“等。
现在我试图从各自的联系人获得相应的联系人图片。所以我已经搜索如何检索联系人的图像和代码示例实现方式如下:
NSData *imgData = nil;
imgData = (NSData *)ABPersonCopyImageData(ref);
contactImage = [UIImage imageWithData:imgData];
现在我已经寻找类似这类问题here和there和常见的答案是保存在应用程序的文件目录和图像该数据库仅保存图像的路径
由于在blob中插入分贝将使我们的数据库非常非常缓慢:(
但我该怎么做这个。我无法理解在我正确地经历过的链接中的代码片段,我该如何转换为UIImage类型的contactImage NSString,以便我可以将字符串插入数据库表中,并将记录保存为VARCHAR类型!
编辑
这是代码建议有:
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);
请帮我,就如何应对挣扎得很厉害并继续前进:(
感谢所有提前:)
哪个代码段你没有得到的形象呢?如果您在Xcode中复制粘贴代码,并通过单击Xcode右侧的每个代码块来查找引用,它非常简单。 –
@anonymous请参阅我的文章的编辑部分 –
我认为@Hanon提供了易于理解的答案。 –