截屏
我正在尝试捕获(屏幕截图)视图。为此,我使用下面所示的一段代码,将其作为 PNG 图像保存到我的文档目录中。
UIGraphicsBeginImageContextWithOptions(highlightViewController.fhView.centerView.frame.size, YES, 1.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"1.png"];
NSData *imageData = UIImagePNGRepresentation(screenshot);
[imageData writeToFile:appFile atomically:YES];
UIGraphicsEndImageContext();
问题:我可以捕捉部分视图吗?因为在上面的代码中我无法更改原点(框架)。如果有人有其他方法来捕获视图的特定部分,请分享。
I am trying to capture (screen shot) a view. For that I am using a piece of code shown below that saves it to my document directory as a PNG image.
UIGraphicsBeginImageContextWithOptions(highlightViewController.fhView.centerView.frame.size, YES, 1.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"1.png"];
NSData *imageData = UIImagePNGRepresentation(screenshot);
[imageData writeToFile:appFile atomically:YES];
UIGraphicsEndImageContext();
Question: can I capture part of the view? Because in the above code I can't change the origin (frame). If anyone has other approach to capture a particular part of view please share it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以裁剪图像:
http://iosdevelopertips.com/graphics/how-to-crop- an-image.html
You could crop the image:
http://iosdevelopertips.com/graphics/how-to-crop-an-image.html
试试这个代码。这确实有效,因为我已经在许多项目中实现了它:
我希望这会对您有所帮助。
Try this code. This surely works as I have implemented it in many of my projects:
I hope this will help you.
看看是否可以像这样指定矩形,然后截图。
您可以更改原点并查看它是否有效。
如果这不起作用,您可以尝试按照@mcb所述裁剪图像
See if you can specify the rect like this and then take screenshot.
You can alter the origin and see if it works.
If this doesn't work out, you can try cropping the image as mentioned by @mcb
您可以使用此代码
You can use this code