iPhone Save image to documents folder task in background thread

Discussion in 'iOS Development' started by jrlipartito, Aug 17, 2012.

  1. jrlipartito

    jrlipartito Newbie

    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:

    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!
     
    Last edited by a moderator: Aug 17, 2012
  2. princess_affair

    princess_affair Level 1 Regular Member

    is this a iPhone app or a Mac app?
     
  3. princess_affair

    princess_affair Level 1 Regular Member

    Why don't you just save a lower quality image as opposed to the high quality one? That will save time and space
     
  4. jrlipartito

    jrlipartito Newbie

    Oh, terribly sorry! This is my first post and I've just realized I posted in the wrong thread. It is an iPhone app.
     
  5. jrlipartito

    jrlipartito Newbie

    Won't the compression/resize activity also take time and block the main thread?
     
  6. boomstick

    boomstick Super Moderator Staff Member

    Last edited by a moderator: Aug 18, 2012
  7. Thanks for sharing regarding this link for iOS 4 Multi tasking, please keep sharing insight…
     
  8. damponting44

    damponting44 Banned

    Prior to iOS 7, only a few kinds of application were allowed to download resources or content on the background while they would not run, and just for a limited time. Big downloads should actually occur while the app was in the foreground, and that was a hard fact for all developers. However, things changed in iOS 7 with the Background Transfer Service coming, as it totally eliminates all the limitations presented above. Not only every app can download content while it’s not running, but it can also have as much time as it’s required at its disposal until all downloads are over.

    Great flexibility and more power comes when the Background Transfer Service is combined with other multitasking features, such as the Background Fetch. For example, using the Background Fetch an app can schedule and initiate a download in the limited time that has at its disposal, and then using the Background Transfer Service to perform the actual data downloading.
     

Share This Page