ios developer
ios developer

Reputation: 3473

Take screenshot from UIVIEW+ipad

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.

Upvotes: 2

Views: 339

Answers (3)

Nikunj
Nikunj

Reputation: 987

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;

}

Upvotes: 1

Muhammed Ayaz
Muhammed Ayaz

Reputation: 1398

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];

Upvotes: 3

Nikhil Bansal
Nikhil Bansal

Reputation: 1545

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 :)

Upvotes: 2

Related Questions