0

I am developing an iPad app in which I let the user create an object. Also I allow him/her to add 10 pictures to this object. They can select these pictures from their camera roll or take a new one (if the iPad has a camera).

My question now is what would be the best way to store these pictures? In my opinion the best way would be to make use of the UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:finishedSavingWithError:contextInfo:), nil); method. This way I can always store (or use) the images in the iPad's photo album. But in this case I'd need to be able to store something in the database to be able to retrieve the image again later on. So... How do I obtain a URL to the file's path, inside the finishedSavingWithError:contextInfo: method, to store in the database?

Another way to achieve my goal would be to save them to a private folder as:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
        NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
        if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
               UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
               imageView.image = image;

               [ImageManager saveImage:image atIndex:0];
       }
}

And my saveImage method does the following:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", index]]; 
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];

Or should I store the images as a BLOB in the sqlite database?

Personally I think these last 2 possibilities blow because basically you're storing everything twice. If you select an image from your gallery, it'll be stored locally (your gallery) PLUS in the database or in an application sub-directory. Which is a waste of space.

What is the best way to do this, performance wise?

And also, if I store them in a directory, can the user access this directory and remove them?

Thanks for sharing your wisdom!

Jules
  • 7,148
  • 6
  • 26
  • 50

1 Answers1

0

I don't think you can get a URL/Path to an image saved using UIImageWriteToSavedPhotosAlbum

Even if this was possible, I would suggest that you still store the images in your app. By exporting them to the Photos app, you would be effectively disassociating the images from your app and the object they were created for.

For example, what happens if someone deleted the image from the Photos app without realizing/remembering that the image is associated with your app? You would have no way of preventing this.

With regards to the method of saving (BLOB or file) either is acceptable. I would suggest that you look at resizing the image before saving unless having the full size original image is required. There are lots of resources for doing this (E.g. UIImage: Resize, then Crop)

Community
  • 1
  • 1
Ian L
  • 5,553
  • 1
  • 22
  • 37
  • I wouldn't need to prohibit them from deleting the picture though. If they do so, it's their mistake and I can just check whether or not the image exists. If not, I won't try to load it. But at least it lets the user organize the images the way they want to, and I wouldn't be using double the memory capacity that's actually needed. – Jules Aug 01 '11 at 22:44
  • Yep, I appreciate that it can be handled very gracefully, I was just thinking of the user experience. (And assuming you could resize the image for the purposes of the app, the memory would be less than doubled). – Ian L Aug 01 '11 at 22:56
  • Thanks for linking me to the useful Resizing thread. I'll go for this solution then. Resizing the image to 1024x768 and storing it as a BLOB into the database. – Jules Aug 03 '11 at 10:28