屏幕截图仅部分屏幕 - Swift
Screenshot Only Part of Screen - Swift
我正在使用此 Swift 代码截取我的应用程序的屏幕截图:
UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, false, 0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
我如何只截取屏幕的一部分,而不是全部,就像我在这里所做的那样?
例如,如果您想在位置 (50,50)
处截取 UIView
矩形的屏幕截图,高度为 (100,150)
。
首先将图像上下文设置为矩形的大小。这将设置图像大小。
UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,150), false, 0);
现在在此上下文中绘制视图,使屏幕截图的 可见部分 从 (50,50)
开始
self.view.drawViewHierarchyInRect(CGRectMake(-50,-50,view.bounds.size.width,view.bounds.size.height) afterScreenUpdates: true)
这会截取 (100,150)
处的屏幕截图,因为我们设置了 context
的大小。现在从中获取 UIImage。
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
我认为这是拍摄部分屏幕的最佳答案:
func screenShot() {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.frame.size.width*0.99,self.frame.size.height*0.70), false, 0)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
self.view?.drawViewHierarchyInRect(CGRectMake(-01, -01, self.frame.size.width, self.frame.size.height), afterScreenUpdates: true)
var screenShot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
用于按另一个 UIView 的区域选择快照
// viewObject: UIView linked from storyboard
var bounds= CGRect(x: -viewObject.frame.minX,y: -viewObject.frame.minY,width: view.bounds.size.width,height: view.bounds.size.height)
UIGraphicsBeginImageContext(viewObject.frame.size)
view.drawHierarchy(in: bounds, afterScreenUpdates: true)
let outputImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
func captureView(view: UIView) -> UIImage {
var screenRect: CGRect = view.bounds
UIGraphicsBeginImageContext(screenRect.size)
UIGraphicsBeginImageContextWithOptions(screenRect.size, true, 0)
var ctx: CGContext = UIGraphicsGetCurrentContext()!
UIColor.black.set()
ctx.fill(screenRect)
view.layer.render(in: ctx)
var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
我正在使用此 Swift 代码截取我的应用程序的屏幕截图:
UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, false, 0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
我如何只截取屏幕的一部分,而不是全部,就像我在这里所做的那样?
例如,如果您想在位置 (50,50)
处截取 UIView
矩形的屏幕截图,高度为 (100,150)
。
首先将图像上下文设置为矩形的大小。这将设置图像大小。
UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,150), false, 0);
现在在此上下文中绘制视图,使屏幕截图的 可见部分 从 (50,50)
self.view.drawViewHierarchyInRect(CGRectMake(-50,-50,view.bounds.size.width,view.bounds.size.height) afterScreenUpdates: true)
这会截取 (100,150)
处的屏幕截图,因为我们设置了 context
的大小。现在从中获取 UIImage。
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
我认为这是拍摄部分屏幕的最佳答案:
func screenShot() {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.frame.size.width*0.99,self.frame.size.height*0.70), false, 0)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
self.view?.drawViewHierarchyInRect(CGRectMake(-01, -01, self.frame.size.width, self.frame.size.height), afterScreenUpdates: true)
var screenShot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
用于按另一个 UIView 的区域选择快照
// viewObject: UIView linked from storyboard
var bounds= CGRect(x: -viewObject.frame.minX,y: -viewObject.frame.minY,width: view.bounds.size.width,height: view.bounds.size.height)
UIGraphicsBeginImageContext(viewObject.frame.size)
view.drawHierarchy(in: bounds, afterScreenUpdates: true)
let outputImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
func captureView(view: UIView) -> UIImage {
var screenRect: CGRect = view.bounds
UIGraphicsBeginImageContext(screenRect.size)
UIGraphicsBeginImageContextWithOptions(screenRect.size, true, 0)
var ctx: CGContext = UIGraphicsGetCurrentContext()!
UIColor.black.set()
ctx.fill(screenRect)
view.layer.render(in: ctx)
var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}