2

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

Community
  • 1
  • 1
Eshwar Chaitanya
  • 697
  • 10
  • 21

1 Answers1

6

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];
}
Hanon
  • 3,917
  • 2
  • 25
  • 29
  • Thanks Mr.Hanon for the answer,will implement and get back to you – Eshwar Chaitanya May 30 '12 at 07:14
  • One small doubt Mr.Hanon,my image name is contactImage,i.e. the retrieved image from contact,so will this line UIImagePNGRepresentation(image) work and also what is that filename,we haven't assigned any value na – Eshwar Chaitanya May 30 '12 at 07:22
  • You pass the contactImage as the first argument, this should be no problem. For second, my function is an example, probably you should pass the person name or something that you use as primary key in your DB. If you pass the name, then you may change to like this, -(void)saveImage:(UIImage *)image forPerson:(NSString *)personName {NSString *filename = [personName stringByAppendingString:@".png"]; ...} – Hanon May 30 '12 at 07:28
  • What I did is I have declared the ref i.e. of type ABRecordRef as global and in the method removed the withName part in the method,and gave ref to path component i.e. AppendingPathComponent:ref,so will it work,is it fine – Eshwar Chaitanya May 30 '12 at 07:37
  • The ref is a struct type and you should get some string value first. And remember to append the string with file extension before appending it to the documents directory. You can log the savedImagePath to see if the path is correct or not – Hanon May 30 '12 at 07:46
  • Sir,what is string type,please explain me in detail,what I did for getting contact name and then image can be seen in edit part of post with title "This is code I implemented",can u please check it in edit part of my post,please – Eshwar Chaitanya May 30 '12 at 07:49
  • We cannot directly give the image name as it is an image from contact or corresponding contact :( – Eshwar Chaitanya May 30 '12 at 07:52
  • Yes you are right. But what you did is you got the full name of that person and is supposed to be unique for your database. So you can just use the full name as the image name. ie. NSString *filename = [fullName stringByAppendingString:@".png"]; NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:filename]; – Hanon May 30 '12 at 07:53
  • Bad excess error Mr.Hanon at AppendingPathComponent:fileName :( – Eshwar Chaitanya May 30 '12 at 08:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11914/discussion-between-hanon-and-eshwar-chaitanya) – Hanon May 30 '12 at 08:06