Search code examples
iphoneiosuiimagepickercontrollercustomizationcamera-overlay

Immediately get UIImage after UIImagepickercontroller -(void)takePicture


I have a cameraOverlay with it's own camera button and I use the takePicture method when they press the camera button and use the delegate method

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info{
    [CommonClass AddToStack:@"ReviewController:imagePickerController"];
    PhotoOverlay *overlay = (PhotoOverlay*)picker.cameraOverlayView;
    [overlay setupImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
}

the setupImage: method just does sets the image for a UIImageView I have on the overlay

the problem with this is that there is a 0.3 second gap between the takePicture and the imagepicker delegate method so it looks really weird. I've seen apps transition immediately from pressing the camera to the final view when using a custom overlay.

has anybody encountered this?


Solution

  • The way to get the instant image is by doing a screenshot of the screen and using that image first, then call the takePicture and replace it with the screenshot when it's ready (the screenshot is a lower resolution than the actual image).

    Here is my snippet:

    -(void)CameraPressedTake
    {
    extern CGImageRef UIGetScreenImage();
    PhotoOverlay *overlay = (PhotoOverlay*)cameraPicker.cameraOverlayView;
    
    CGImageRef cgoriginal = UIGetScreenImage();
    
    float width = 320.0;
    float height = 480.0-[overlay myHeight];
    //if it's retina...
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2) {
        width = 2*width;
        height = 2*height;
    }
    CGImageRef cgimg = CGImageCreateWithImageInRect(cgoriginal, CGRectMake(0, 0, width, height));            
    UIImage *viewImage = [UIImage imageWithCGImage:cgimg];
    [overlay setupImage:viewImage];
    CGImageRelease(cgoriginal);                
    CGImageRelease(cgimg);
    [cameraPicker takePicture];
    }