捕获屏幕

aja*_*jay 7 iphone screenshot objective-c ios

我试图捕捉(屏幕截图)一个视图.为此,我使用下面显示的一段代码将其作为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();
Run Code Online (Sandbox Code Playgroud)

问题:我可以捕获部分视图吗?因为在上面的代码我不能改变原点(框架).如果有人有其他方法来捕获视图的特定部分,请分享.

Len*_*oyt 6

您可以裁剪图像:http: //iosdevelopertips.com/graphics/how-to-crop-an-image.html

CGRect rect = CGRectMake(0,0,10,10);
CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], rect);
UIImage *croppedScreenshot = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);
Run Code Online (Sandbox Code Playgroud)