2

I want to take the screenhot of viewcontroller I had write this:-

- (UIImage *)captureScreenInRectWOScroll:(CGRect)captureFrame {    
    CALayer *layer;
    layer = self.view.layer;
    UIGraphicsBeginImageContext(captureFrame.size); 
    CGContextClipToRect (UIGraphicsGetCurrentContext(),captureFrame);
    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenImage; 
}

The way i am calling this method:-

UIImage *img_woscroll1 =[self captureScreenInRectWOScroll:CGRectMake(35,147,497,260)];

I wann to take screenshot of Address from below attached image:- enter image description here

When i am taking hte screenshot from the above code i Got the image with lot of blank space(greencolor image at top) at top side if image:-

enter image description here

Please help me..How to take proper screenshot from image such that i will not get blank image on topside.

ios developer
  • 3,363
  • 3
  • 51
  • 111

3 Answers3

3

Try This

UIGraphicsBeginImageContext(myView.frame.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data=UIImagePNGRepresentation(viewImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *strPath = [documentsDirectory stringByAppendingPathComponent:@"myimage.png"];
[data writeToFile:strPath atomically:YES];
Ayaz
  • 1,398
  • 15
  • 29
2

Try This:-

CGRect screenRect = CGRectMake(0, 90, 320,460);
UIGraphicsBeginImageContext(screenRect.size);

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextFillRect(ctx, screenRect);

//you probably need an offset, adjust here
CGContextTranslateCTM(ctx, -20, -20);
[self.view.layer renderInContext:ctx];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

It may help you. Thanks :)

Nikhil Bansal
  • 1,545
  • 13
  • 29
1

Try with this. This may help you.

- (UIImage *)captureView:(UIView *)view {
CGRect screenRect = [[UIScreen mainScreen] bounds];

UIGraphicsBeginImageContext(screenRect.size);

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

[view.layer renderInContext:ctx];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;

}

Nikunj
  • 987
  • 11
  • 25
  • i Find it here. http://stackoverflow.com/questions/879064/how-to-capture-current-view-screenshot-and-reuse-in-code-iphone-sdk – Nikunj Jun 08 '12 at 10:55
  • thanks for the reply.But Which view should i pass in Method?can u please suggest me – ios developer Jun 08 '12 at 11:08
  • I think you can pass self.view or self.navigationController.view. I think both can work pls check. – Nikunj Jun 08 '12 at 11:31