Eshwar Chaitanya
Eshwar Chaitanya

Reputation: 697

Store images in to sqlite database

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

Upvotes: 2

Views: 4304

Answers (1)

Hanon
Hanon

Reputation: 3927

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

Upvotes: 6

Related Questions