Jules
Jules

Reputation: 7223

Store and use images coming from the Camera or Album in iPad app

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!

Upvotes: 0

Views: 1734

Answers (1)

Ian L
Ian L

Reputation: 5601

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)

Upvotes: 0

Related Questions