Search code examples
iosobjective-cimageimage-capture

Capture image from UIView is not working on iOS 10.3.3


I capture image from UIView content like below and it's working fine but when i run it in my iPhone5S with iOS 10.3.3, Captured image contains only Black View.

-(UIImage *)captureImageFromView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

What's the issue? Thanks in Advance.


Solution

  • You can do that way as explained by @LGP.

    Use following function on main thread like below.

    -(UIImage *)captureImageFromView:(UIView *)view {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
        [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return image;
    }
    

    Main thread code.

    dispatch_async(dispatch_get_main_queue(), ^{
        UIImage *imgResult = [self captureImageFromView:yourView];
    });