I'm quite new to iOS programming. I've been working on a pretty simple camera app, but one major function I need to accomplish is to not only display an image taken with the built-in camera or picked from the iPhone gallery with a UIImageView, but also save it to the documents folder so it can be called in a second view controller later on. This I've managed to accomplish with the following code:
//Save taken image to Camera Roll and display on UIImageView
-(void)imagePickerController:(UIImagePickerController *)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:YES];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info
objectForKey:UIImagePickerControllerOriginalImage];
imageView.image = image;
imageView.hidden = NO;
if (newMedia)
UIImageWriteToSavedPhotosAlbum(image,
self,
@selector(image:finishedSavingWithError:contextInfo:),
nil);
}
//Save photo to documents folder
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
}}
The problem I've run into is that the process of saving the image to the documents folder takes so long (>5sec!) that the user would think the app is hanging. Of course I want to display an activity indicator, but have now learned this isn't possible because the save to documents folder task is occupying my main thread, thus blocking any other UIView task from executing until saving is complete.
It seems like the solution is moving save to documents task to a background thread, but at the moment I can't quite wrap my head around how to do that. Does anyone know a some simple code I can add on to the above code to move this task to the background? Or maybe someone could refer me to a good tutorial on this?
EDIT:
Here's something else I've tried. I've set up @jake_hetfield 's saveImage method and placed just the save to documents code inside like so:
- (void)saveImage:(UIImage*)image {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory
stringByAppendingPathComponent:@"savedImage.png"];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
[self performSelectorInBackground:@selector(saveImage:) withObject:image]; }
But the line UIImage *image = imageView.image; is suddenly returning the build error 'Redefinition of image'. This is confusing because that same line works when I ran the save to documents code in the PickerDidFinish... method. Any ideas of how I can fix this error?
Thanks in advance!