1

I am having some troubles saving images path into Documents folder and store the path into my sqlite3 local db.

Today I successfully stored the images as BLOB into the db, but reading around the Internet, people said that this is not recommended.

So, now I'm trying to store image path in DB but..

Situation

  1. User taps a button to choose an image (via UIImagePickerView) or take a new photo
  2. After choosing the image, an imageView is set with the image chosen.

Code for picker:

-(void)imagePickerController:(UIImagePickerController *)pickr didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    /* Test code for saving data */   
    //NSString *dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    //NSString *pngPath = [NSString stringWithFormat:@"%@/test.png",dir];
    //NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];
    //[data writeToFile:pngPath atomically:YES];

    [imageView setImage:image];
    picture = YES;
    [pickr dismissModalViewControllerAnimated:YES];
}

Now I got a couple of questions

  1. Into my db, image is BLOB type. Should I edit it to TEXT?
  2. How is the path saved? I mean, in the test code /test.png is a generic image name. Does the chosen image name get saved too so that I can retrieve it? Or, better, if I save an image picked from my library with name "IMG0001", does it get saved as "test"?
  3. What's the right way to save an image into documents folder, its path to the DB and then retrieve it?

I googled a lot to find an answer, but after experimenting a lot I gave up.

Thanks in advance

Phillip
  • 4,276
  • 7
  • 42
  • 74

2 Answers2

1

Here is nice tutorial on how to save images in Documents directory

You need to convert your BLOB field to TEXT field and just save file name of that image. You can also create folder in Documents directory and then access by foldername/filename.png.

Hope this information helps you..

EDIT

Here is code to check if that folder exits, If not exist create new that folder

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"MyFolder"];

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){

    NSError* error;
    if(  [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error])
        ;// success
        else
        {
            NSLog(@"[%@] ERROR: attempting to write create MyFolder directory", [self class]);
            NSAssert( FALSE, @"Failed to create directory maybe out of disk space?");
        }
}
P.J
  • 6,547
  • 9
  • 44
  • 74
  • Thanks for the tutorial link, I saw that few minutes ago and I planned to follow it. Thanks for pointing me on the right path, so I guessed right: converting from BLOB to TEXT. – Phillip Jan 10 '13 at 13:49
  • yes, its really very simple and fast to save images in documents directory rather that sqlite database – P.J Jan 10 '13 at 13:53
  • But, as I was saying in the comment below, how can I know the filename (png)? I'm doing the saving stuff in the image picker delegate method, so i'm a bit confused – Phillip Jan 10 '13 at 13:56
  • Its simple, you can take unique id from database after inserting record, and make that id as filename.png and similar way you can access that image – P.J Jan 10 '13 at 13:59
  • with this you dont need to save extra field of filename in your database – P.J Jan 10 '13 at 13:59
  • I'll try to do this way then – Phillip Jan 10 '13 at 14:02
0

You cannot directly access the image from the user's photo library, since you have access only to your Applications Sandbox folder.

So if you don't want to store the image as a blob file in the database, you should save the image in the caches folder inside your App's documents folder and then store the filename you set to the image in the database.

Since all images are going to be in the same path folder, you don't need to actually save the entire path in the database, but only the filename should be sufficient enough.

Lefteris
  • 14,550
  • 2
  • 56
  • 95
  • And this is the part which is still unclear for me. How can I save the filename? And why in the caches folder and not in the Documents? If I save them in caches, and I release an update for the app, the images will be destroyed no? – Phillip Jan 10 '13 at 13:48
  • You are asking me, how to save a file? If yes, there are a lot of topics in SO, just do a search. As for the permanent vs temporary storage, please see this post: http://stackoverflow.com/a/8209801/312312 – Lefteris Jan 10 '13 at 14:01
  • No, I'm not asking you how to save a file, just the name of the file. – Phillip Jan 10 '13 at 14:03
  • But when you save a file to the documents or caches directory, you are specifying the name yourself... – Lefteris Jan 10 '13 at 14:40
  • So if, in the example, it's specified "test.png" all images are named "test.png" or "test1.png","test2.png" and so on? – Phillip Jan 10 '13 at 14:41
  • You are free to select the file naming pattern your self. If the testX.png suites you then yes. But the name will have to be unique, but you can do a while condition to check against unique name before saving. – Lefteris Jan 10 '13 at 14:45
  • Okay, that's what I still didn't understand.. Thanks – Phillip Jan 10 '13 at 14:49