Take screenshot from UIImage iOS

I want to take a screenshot in the percussion section of my screen in an iOS application. The attached image is part of the screen. Here I want to take a screenshot with a red marked rectangular area containing 3 UIImageViews that contain a white frame for the image on the background, the image with a cup of coffee and the image of the apple icon for coffee, respectively.

enter image description here

I am using the following code for this ...

- (UIImage *)captureView:(UIView *)view withArea:(CGRect)screenRect {

    UIGraphicsBeginImageContext(screenRect.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    CGContextFillRect(ctx, screenRect);

    [view.layer renderInContext:ctx];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage ;
}

Call this type

UIImage *viewImage = [self captureView:self.FrameImageView withArea:self.FrameImageView.bounds];

Here, FrameImageView is a UIImageView that contains a white frame of the image in the background.

But I get the resulting image as shown below.

enter image description here

, , FrameImageView. . , 2 UIImageViews FrameImageView, .

- , . Advance.

+1
2

 UIGraphicsBeginImageContextWithOptions(FrameImageView.frame.size, YES, 4);

 [_myStreetView drawViewHierarchyInRect:FrameImageView.frame afterScreenUpdates:YES];

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

 UIImageView *img = [[UIImageView alloc] initWithImage:image];
+2

:

- (UIImage*)captureView:(UIView *)yourView {
    CGRect rect = [[UIScreen mainScreen] bounds];
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [yourView.layer renderInContext:context];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
+2

All Articles