1

I want to take screenshot of a perticular section of my screen in a iOS App. The attached image is that part of screen. Here I want to take screenshot of the red marked rectangular area containing 3 UIImageViews which contains a white Image Frame at background , an image with coffee cup and the apple sign image on the coffee respectively.

enter image description here

I am using the following code for doing that...

- (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 ;
}

Calling it like this

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

Here FrameImageView is an UIImageView that contains the white Image frame at the background.

But I am getting the resulting image like the following.

enter image description here

I think the problem is , my code is taking screenshot of the layer of FrameImageView. That's why I am getting the background frame only. But I want the screenshot to contain the other 2 UIImageViews over FrameImageView as well like the first image's red rectangle section.

can anyone help me , how to fix that. Thanks in Advance.

ayon
  • 2,180
  • 2
  • 17
  • 32

2 Answers2

2

Try this variant

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

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

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

 UIImageView *img = [[UIImageView alloc] initWithImage:image];
nerowolfe
  • 4,787
  • 3
  • 20
  • 19
  • Thanks...but what's _myStreetView here ? – ayon Nov 30 '13 at 10:12
  • I have tested the code , please see it's output here...http://i.imgur.com/Z58xuLz.png – ayon Nov 30 '13 at 10:23
  • Actually, I think you need to make a screenshot of self.view or any parent view with image views contents. And the crop the result to red rectangle – nerowolfe Nov 30 '13 at 10:30
  • I also think so. And my code takes screenshot of self.view perfectly. but when cropping the image , I can't get the actual CGRect of the FrameImageView. FrameImageView.frame.bounds gives (0,0,180,180) , so cropped image starts from top left of parent view. Not sure if I am making any mistake to get the CGrect to get position of FrameImageView. Can you please help me with that ? – ayon Nov 30 '13 at 10:35
  • FrameImageView's parent view is self.view? – nerowolfe Nov 30 '13 at 10:52
  • I have managed to solve my problem with my code. Everything is fine now. Thanks for your help. – ayon Nov 30 '13 at 11:01
2

Try this:

- (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;
}
spaleja
  • 1,435
  • 15
  • 24