Ios Swift:在没有TabBar和NavigationBar的情况下裁剪屏幕截图
我有一个整个屏幕的屏幕截图,Ios Swift:在没有TabBar和NavigationBar的情况下裁剪屏幕截图,ios,swift,screenshot,cgimage,uigraphicscontext,Ios,Swift,Screenshot,Cgimage,Uigraphicscontext,我有一个整个屏幕的屏幕截图,屏幕截图,使用以下方法生成: let layer = UIApplication.sharedApplication().keyWindow!.layer let scale = UIScreen.mainScreen().scale UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale); layer.renderInContext(UIGraphicsGetCurrentCon
屏幕截图
,使用以下方法生成:
let layer = UIApplication.sharedApplication().keyWindow!.layer
let scale = UIScreen.mainScreen().scale
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
layer.renderInContext(UIGraphicsGetCurrentContext())
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
我希望对其进行裁剪,以便不包括选项卡栏,并尝试使用以下代码:
let crop = CGRectMake(0, 0, //"start" at the upper-left corner
self.view.bounds.width, //include half the width of the whole screen
self.view.bounds.height + self.navigationController!.navigationBar.frame.height) //include the height of the navigationBar and the height of view
let cgImage = CGImageCreateWithImageInRect(screenshot.CGImage, crop)
let image: UIImage = UIImage(CGImage: cgImage)!
此代码导致图像
仅显示屏幕的一小部分,一个从屏幕左上角(0,0)开始的矩形,向右延伸不到屏幕宽度的一半,然后向下延伸不到屏幕高度的一半。不过,我希望它包括整个屏幕,除了选项卡栏占据的区域。有没有这样的方法来修剪它 swift:
let contextImage: UIImage = <<screenshot>>!
let cropRect: CGRect = CGRectMake(x, y, width, height)
let imageRef: CGImageRef = CGImageCreateWithImageInRect(contextImage.CGImage, cropRect)
let image: UIImage = UIImage(CGImage: imageRef, scale: originalImage.scale, orientation: originalImage.imageOrientation)!
let contextImage:UIImage=!
设cropRect:CGRect=CGRectMake(x,y,宽度,高度)
让imageRef:CGImageRef=CGImageCreateWithImageInRect(contextImage.CGImage,cropRect)
让image:UIImage=UIImage(CGImage:imageRef,scale:originalImage.scale,orientation:originalImage.imageOrientation)!
obj-c:
UIImage *image = <<screenshot>>;
CGRect croprect = CGRectMake(0, 0,
self.view.bounds.width,
self.view.bounds.height + self.navigationController!.navigationBar.frame.height));
// Draw new image in current graphics context
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], croprect);
// Create new cropped UIImage
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
UIImage*image=;
CGRect croprect=CGRectMake(0,0,
self.view.bounds.width,
self.view.bounds.height+self.navigationController!.navigationBar.frame.height));
//在当前图形上下文中绘制新图像
CGImageRef imageRef=CGImageCreateWithImageInRect([image CGImage],croprect);
//创建新的裁剪图像
UIImage*croppedImage=[UIImage imageWithCGImage:imageRef];
这里有两个选项:
首先,您可以使用UIApplication.sharedApplication().keyWindow拍摄屏幕截图,然后我们将获得以下内容:
UIGraphicsBeginImageContext(UIApplication.sharedApplication().keyWindow!.bounds.size)
UIApplication.sharedApplication().keyWindow!.layer.renderInContext(UIGraphicsGetCurrentContext())
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
因此,您必须按如下方式裁剪图像:
let yPosition = self.navigationController!.navigationBar.frame.height + UIApplication.sharedApplication().statusBarFrame.size.height
let crop = CGRectMake(0, yPosition,
self.view.bounds.width,
self.view.bounds.height)
let cgImage = CGImageCreateWithImageInRect(screenshot.CGImage, crop)
let image: UIImage = UIImage(CGImage: cgImage)!
UIGraphicsBeginImageContext(self.view!.bounds.size)
self.view!.layer.renderInContext(UIGraphicsGetCurrentContext())
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
第二个选项是直接获取视图的屏幕截图,如下所示:
let yPosition = self.navigationController!.navigationBar.frame.height + UIApplication.sharedApplication().statusBarFrame.size.height
let crop = CGRectMake(0, yPosition,
self.view.bounds.width,
self.view.bounds.height)
let cgImage = CGImageCreateWithImageInRect(screenshot.CGImage, crop)
let image: UIImage = UIImage(CGImage: cgImage)!
UIGraphicsBeginImageContext(self.view!.bounds.size)
self.view!.layer.renderInContext(UIGraphicsGetCurrentContext())
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
在这种情况下,不需要裁剪导航栏
这是github的一个示例代码我认为您的裁剪CGRect
是错误的
let crop = CGRectMake(0,
0,
self.view.frame.size.width,
self.view.frame.size.height + self.navigationController!.navigationBar.frame.height - self.tabBar.frame.size.height
)
X:0和Y:0-分别从0和0开始
宽度:捕获视图的整个宽度
高度:整个视图的高度加上UINavigationBar的高度
减去uitabar的高度
新图像由
1) 通过调用CGRectIntegral
将rect
调整为整数范围
2) 将结果与原点为(0,0)且大小为的矩形相交
等于图像的大小
3) 引用结果矩形内的像素,处理
作为图像原点的图像数据的第一个像素。
如果生成的矩形为空矩形,则此函数返回
空
如果W和H分别是图像的宽度和高度,则
点(0,0)对应于图像数据的第一像素;重点
(W-1,0)是图像数据的第一行的最后一个像素;(0,H-1)
是图像数据的最后一行的第一像素;(W-1,H-1)是
图像数据最后一行的最后一个像素
您需要有这样的裁剪功能。您可能需要调整bottomBarHeight
func takeScreenshot(sender: AnyObject) {
let layer = UIApplication.sharedApplication().keyWindow!.layer
let scale = UIScreen.mainScreen().scale
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
layer.renderInContext(UIGraphicsGetCurrentContext())
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let croppedImage = self.cropImage(screenshot)
}
func cropImage(screenshot: UIImage) -> UIImage {
let scale = screenshot.scale
let imgSize = screenshot.size
let screenHeight = UIScreen.mainScreen().applicationFrame.height
let bound = self.view.bounds.height
let navHeight = self.navigationController!.navigationBar.frame.height
let bottomBarHeight = screenHeight - navHeight - bound
let crop = CGRectMake(0, 0, //"start" at the upper-left corner
(imgSize.width - 1) * scale, //include half the width of the whole screen
(imgSize.height - bottomBarHeight - 1) * scale) //include the height of the navigationBar and the height of view
let cgImage = CGImageCreateWithImageInRect(screenshot.CGImage, crop)
let image: UIImage = UIImage(CGImage: cgImage)!
return image
}
我在Swift 4.2中的回答是,步骤在函数getScreenshot()
这也是公认答案的Swift 4.2版本:
func takeScreenshot(sender: AnyObject) {
let layer = UIApplication.shared.keyWindow!.layer
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
layer.render(in: UIGraphicsGetCurrentContext()!)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let croppedImage = self.cropImage(screenshot: screenshot!)
}
func cropImage(screenshot: UIImage) -> UIImage {
let scale = screenshot.scale
let imgSize = screenshot.size
let screenHeight = UIScreen.main.bounds.height
let bound = self.view.bounds.height
let navHeight = self.navigationController!.navigationBar.frame.height
let bottomBarHeight = screenHeight - navHeight - bound
let crop = CGRect(x: 0, y: 0, width: (imgSize.width - 1) * scale, height: (imgSize.height - bottomBarHeight - 1) * scale)
let cgImage = screenshot.cgImage?.cropping(to: crop)
let image: UIImage = UIImage(cgImage: cgImage!)
return image
}
你关于裁剪图像的建议似乎没有达到我想要的效果,它仍然只是屏幕左上角的一小部分。这会裁剪出导航栏,而不是选项卡栏。我只是对你的答案进行了投票,但我只是意识到这并不排除选项卡。这个问题正在Swift中寻找答案,不客观-我的坏。。。我补充说雨燕公司这对我来说仍然不起作用,我仍然只看到左上象限。这可能与我正在使用的显示器(iPhone6Plus)有关吗?你在哪里调用这个代码?视图控制器?子视图?视图控制器,特别是导航栏按钮的动作。您所说的“左上象限”是什么意思?您是指整个屏幕的左上象限吗?只有按钮吗?你能发布一个屏幕截图和一个独立的项目来重现这个问题吗?