且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将图像存储到sqlite数据库中

更新时间:2022-10-18 18:40:50

将图像保存到文档

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