Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

iPhone Save image to documents folder task in background thread

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?

Thanks in advance!
Post edited by jrlipartito on
Tagged:
Sign In or Register to comment.