[英]Taking screenshot of Part of Screen
我的视图控制器中有一个UIImageView,需要抓取其screeshot。 上面也有一个UILabel。 UIImageView并不覆盖整个页面,只是一部分。 我该如何截取屏幕上一部分的屏幕截图?
我敢打赌,以前曾问过这个问题,但我调查了这篇文章。 如何使用UIGraphicsBeginImageContextWithOptions? 它只不过与未定义的变量混淆
到目前为止,我所拥有的代码已截取了整个内容的屏幕快照。 (不是我想要的)。 我尝试使用screenRect无效。 我考虑过在拍摄整个屏幕截图后裁剪图片,但是问题是,每种设备的裁剪大小都会有所不同,例如iphone3,iphone4,iphone5,iPad 1,2、3等。
所以我的问题是,有一种方法可以截取一部分屏幕的屏幕截图,而无需经过裁切路线吗? 如果裁切是唯一的方法,那么有没有办法我可以可靠地剪切图片而不必担心具有高/低像素的其他iOS设备?
testBgImgVw = [[UIImageView alloc] initWithFrame:CGRectMake(0, 80, 320, 220)];
....
-(IBAction) takeScreenshot
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
//its iphone
screenRect = CGRectMake(0, 0, 320, 480);
}
else
{
//its pad
screenRect = CGRectMake(0, 0, 768, 1024);
}
// This code is for high resolution screenshot
UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData* imageData = UIImageJPEGRepresentation(viewImage, 1.0);
NSString* incrementedImgStr = [NSString stringWithFormat: @"UserScreenShotPic.jpg"];
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
// Now we get the full path to the file
NSString* fullPathToFile2 = [documentsDirectory stringByAppendingPathComponent:incrementedImgStr];
[imageData writeToFile:fullPathToFile2 atomically:NO];
}
好的,如果您只想捕获图像和图像上的标签(如果可以确保标签始终在图像上),则
在imageview上添加标签(imageview addSubview:label)
然后将其渲染为新图像
UIGraphicsBeginImageContextWithOptions(screenRect.size, NO,[[UIScreen mainScreen]scale]);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
完成:)
如果您想拍摄几个uiview(imageview,label等),最好的方法是提供一个UIView画布
UIView *canvasView = [[UIView alloc]......]
[self.view addSubview:canvasView];
并添加您要在其上渲染的每个视图,最后将其渲染为所需的新图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.