我希望用户在Swift中以编程方式按下按钮后继续运行我的应用并为应用截图。我知道UIGraphicsGetImageFromCurrentImageContext()
需要截图,但是我不需要整个屏幕的图片。我希望弹出一个矩形(有点像裁剪工具),并且用户可以拖动矩形并调整其大小以仅截取屏幕的特定部分的屏幕截图。我希望矩形经过a
WKWebView
并裁剪Web视图的图片。
标准的快照技术是drawHierarchy(in:afterScreenUpdates:)
,将其绘制到图像上下文。在iOS
10及更高版本中,您可以使用UIGraphicsImageRenderer
:
extension UIView {
/// Create image snapshot of view.
///
/// - Parameters:
/// - rect: The coordinates (in the view's own coordinate space) to be captured. If omitted, the entire `bounds` will be captured.
/// - afterScreenUpdates: A Boolean value that indicates whether the snapshot should be rendered after recent changes have been incorporated. Specify the value false if you want to render a snapshot in the view hierarchy’s current state, which might not include recent changes. Defaults to `true`.
///
/// - Returns: The `UIImage` snapshot.
func snapshot(of rect: CGRect? = nil, afterScreenUpdates: Bool = true) -> UIImage {
return UIGraphicsImageRenderer(bounds: rect ?? bounds).image { _ in
drawHierarchy(in: bounds, afterScreenUpdates: afterScreenUpdates)
}
}
}
而且您会这样使用:
let image = webView.snapshot(of: rect)
在iOS 10之前,您需要获取图像的一部分,可以使用CGImage
method
cropping(to:)
。例如:
extension UIView {
/// Create snapshot
///
/// - Parameters:
/// - rect: The coordinates (in the view's own coordinate space) to be captured. If omitted, the entire `bounds` will be captured.
/// - afterScreenUpdates: A Boolean value that indicates whether the snapshot should be rendered after recent changes have been incorporated. Specify the value false if you want to render a snapshot in the view hierarchy’s current state, which might not include recent changes. Defaults to `true`.
///
/// - Returns: Returns `UIImage` of the specified portion of the view.
func snapshot(of rect: CGRect? = nil, afterScreenUpdates: Bool = true) -> UIImage? {
// snapshot entire view
UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0)
drawHierarchy(in: bounds, afterScreenUpdates: afterScreenUpdates)
let wholeImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// if no `rect` provided, return image of whole view
guard let image = wholeImage, let rect = rect else { return wholeImage }
// otherwise, grab specified `rect` of image
guard let cgImage = image.cgImage?.cropping(to: rect * image.scale) else { return nil }
return UIImage(cgImage: cgImage, scale: image.scale, orientation: .up)
}
}
使用以下便利操作符:
extension CGRect {
static func * (lhs: CGRect, rhs: CGFloat) -> CGRect {
return CGRect(x: lhs.minX * rhs, y: lhs.minY * rhs, width: lhs.width * rhs, height: lhs.height * rhs)
}
}
并使用它,您可以执行以下操作:
if let image = webView.snapshot(of: rect) {
// do something with `image` here
}
问题内容: 我有一个名为overView的UIView: 我只想截取此视图的屏幕截图,而不是整个屏幕。并制作尺寸截图: 这是我的代码: 屏幕截图具有所需的大小,但是它需要整个视图的屏幕截图,而不仅仅是“ overView”。 问题答案: 要绘制一个视图,只需使用以下命令: 如果您想多次使用它,可能扩展可以完成这项工作: // Swift4 //旧的Swift 要解释这些参数的作用: UIGraph
问题内容: 我为此进行了很多搜索,但找不到解决方案。这是java中可能的解决方案的类似问题。 Python中有类似的解决方案吗? 问题答案: 除了硒以外,此示例还需要PIL映像库。有时将其作为标准库之一放入,有时却不作为,但如果没有,则可以使用 最后输出是… Stackoverflow徽标!!! 当然,现在仅获取静态图像将是过大的选择,但是如果您想要获取需要Javascript才能实现的功能,那可
问题内容: 是否可以使用JavaScript截取网页的屏幕截图,然后将其提交回服务器? 我不太担心浏览器的安全性问题。等,因为实施将针对HTA。但是有可能吗? 问题答案: 我已经通过使用ActiveX控件为HTA完成了此操作。在VB6中构建控件以截取屏幕截图非常容易。我必须使用keybd_event API调用,因为SendKeys无法执行PrintScreen。这是该代码: 这只会使您到达将窗口
在Linux下有很多屏幕载图的工具,下面简单介绍一下: 在GNOME桌面中自带了一个屏幕截图工具,位于“动作”栏内。该工具功能很少,只能截取当前屏幕。 在GMIP中也可截图,在“文件”--“获取”菜单下有一个“屏幕抓图”选项可进行屏幕截图。它可截取任意图窗口的内容,并自动输入到GMIP中,我们可方便地进行处理和保存。 安装ImageMagick软件,它有一个工具叫import可用于屏幕截图。该工具
点击按钮进行截屏,可以将截屏图像保存到相册中。 作者说:听说会和苹果的策略有冲突,应用如果上架可能会被拒绝。这个估计是看人品了吧。经过测试发现,如果先弹出对话框,然后再截屏,似乎并不能把对话框也给保存下来。 [Code4App.com]
我正在使用AlarmManager和Pending Intent调用BroadcastReceiver类。这是每天安排的。 以下是“活动”中调用的BroadCast Receiver类的代码(它是一个单独的类)。 问题陈述是,在此接收器中,我正在打开另一个应用程序,我想捕获屏幕截图并将其上传到服务器。但是窗口功能在广播接收器类中不可用,我无法实现,因为我超出了活动控制。 关键挑战: -在以下类中实