How to add an overlay view to the camera and save it

Thanks in advance. I want to add an image as an overlay to the camera and save both the image (cameraview and image) as a single image. I searched for it and tried the example specified in stackoverflow, but it does not display anything but a blank screen. If anyone knows, please help me.

0
source share
1 answer
- (void)renderView:(UIView*)view inContext:(CGContextRef)context
{
    // -renderInContext: renders in the coordinate space of the layer,
    // so we must first apply the layer geometry to the graphics context
    CGContextSaveGState(context);
    // Center the context around the window anchor point
    CGContextTranslateCTM(context, [view center].x, [view center].y);
    // Apply the window transform about the anchor point
    CGContextConcatCTM(context, [view transform]);
    // Offset by the portion of the bounds left of and above the anchor point
    CGContextTranslateCTM(context,
                          -[view bounds].size.width * [[view layer] anchorPoint].x,
                          -[view bounds].size.height * [[view layer] anchorPoint].y);

    // Render the layer hierarchy to the current context
    [[view layer] renderInContext:context];

    // Restore the context
    CGContextRestoreGState(context);
}

// this get called when an image has been taken from the camera
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
    //cameraImage = image;
    CGSize imageSize = [[UIScreen mainScreen] bounds].size;
    if (NULL != UIGraphicsBeginImageContextWithOptions)
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    else
        UIGraphicsBeginImageContext(imageSize);

    CGContextRef context = UIGraphicsGetCurrentContext();


    // Draw the image returned by the camera sample buffer into the context. 
    // Draw it into the same sized rectangle as the view that is displayed on the screen.
    float menubarUIOffset = 44.0;
    UIGraphicsPushContext(context);
    [image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height-menubarUIOffset)];
    UIGraphicsPopContext();

    // Render the camera overlay view into the graphic context that we created above.
    [self renderView:self.imagePicker.view inContext:context];

    // Retrieve the screenshot image containing both the camera content and the overlay view
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil);
    UIGraphicsEndImageContext();
    [self.imagePicker dismissModalViewControllerAnimated:YES];

}

Hope this helps

+1
source

Source: https://habr.com/ru/post/1728725/


All Articles