2012-05-30 95 views
2

我知道这个问题被很多人问及,有关“为什么要使用sqlite,使用FMDB,核心数据等等”的讨论和争论等等来存储或插入图像到数据库中。但请理解我的问题,我非常习惯sqlite3,我无法使用核心数据或其他数据库。另外我只是在学习iphone技术阶段。所以我请求提供给我一个解决方案,可以是处理sqlite只);将图像存储到sqlite数据库

道歉,如果在我的请求有任何错误!

我有一个文本字段,它会在输入字母时显示带有建议的表格,从联系人,说如果我键入字母“L”,“Lakshaman饶,Prasanna Lakshmi,”Lokesh Sharan“等。

现在我试图从各自的联系人获得相应的联系人图片。所以我已经搜索如何检索联系人的图像和代码示例实现方式如下:

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

现在我已经寻找类似这类问题herethere和常见的答案是保存在应用程序的文件目录和图像该数据库仅保存图像的路径

由于在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); 

请帮我,就如何应对挣扎得很厉害并继续前进:(

感谢所有提前:)

+0

哪个代码段你没有得到的形象呢?如果您在Xcode中复制粘贴代码,并通过单击Xco​​de右侧的每个代码块来查找引用,它非常简单。 –

+0

@anonymous请参阅我的文章的编辑部分 –

+0

我认为@Hanon提供了易于理解的答案。 –

回答

6

为了节省图像文件

- (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]; 
} 
+0

感谢哈龙先生的回答,将执行并取回给你 –

+0

一个小小的怀疑哈龙先生,我的形象名字是contactImage,即从联系人中检索到的图像,所以将这行UIImagePNGRepresentation(图像)的工作,也是什么文件名,我们还没有分配任何价值na –

+0

请看我的代码,我如何获取联系人图片 –