简体   繁体   English

从UIVIEW + ipad截屏

[英]Take screenshot from UIVIEW+ipad

I want to take the screenhot of viewcontroller I had write this:- 我想看一下我写的viewcontroller的屏幕截图:

- (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:- 我想从所附图片中截取地址的屏幕截图:- 在此处输入图片说明

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:- 当我从上面的代码中截取屏幕截图时,如果图像是图像,则图像的顶部有很多空白(顶部是绿色图像):

在此处输入图片说明

Please help me..How to take proper screenshot from image such that i will not get blank image on topside. 请帮助我..如何从图像采取适当的屏幕截图,这样我就不会在顶部得到空白图像。

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

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

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;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM