- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当有硬件加速 内容(一些在 iframe 中运行的特定赌场游戏)时,我在截取 WKWebview 内容时遇到了严重的问题。到目前为止,我按照大家的建议使用了标准的截屏方式:
UIGraphicsBeginImageContextWithOptions(containerView.frame.size, true, 0.0)
containerView.layer.render(in: UIGraphicsGetCurrentContext()!)
//This line helps to fix view rendering for taking screenshot on older iOS devices
containerView.drawHierarchy(in: containerView.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
在我的 WKWebview 中获得一些由 GPU 呈现的内容之前,此方法非常有效。GPU 渲染的内容在屏幕截图上显示为黑色。我尝试了使用此方法可能的所有技巧,但无济于事。即使是 XCode View 层次结构调试器也无法显示硬件加速内容。因此,与 Android 类似,我需要另一种截屏方式。 我已经解决了 Android 上的类似问题,方法是开始记录屏幕上发生的一切并在获得第一张图像后停止屏幕记录。
我经历过无数 Stack Overflow 问题和解决方案,但它们大部分都是 Obj-C(我完全不喜欢),已经过时或不够具体,无法满足我的需求。
现在我发现,我可以使用 glReadPixels 直接从 OpenGL 读取像素(如果我的内容是硬件加速的,那么我可以从显卡读取这些像素是有意义的,对吧??)
到目前为止,我已经成功地创建了一个 Swift 片段,它可以执行类似renderBuffer -> frameBuffer -> glReadPixels -> image
的操作let width = Int(containerView.frame.size.width)
let height = Int(containerView.frame.size.height)
//BeginImageContext code was run above
let api = EAGLRenderingAPI.openGLES3
let context2 = EAGLContext(api: api)
EAGLContext.setCurrent(context2)
// Setup render buffer
var renderBuffer : GLuint = GLuint()
let size = GLsizei(10)
glGenRenderbuffers(size, &renderBuffer)
glBindRenderbuffer(GLenum(GL_RENDERBUFFER), renderBuffer)
let bufferWidth = GLsizei(width * 1)
let bufferHeight = GLsizei(height * 1)
let bufferFormat = GLenum(GL_RGBA8)
glRenderbufferStorage(GLenum(GL_RENDERBUFFER), bufferFormat, bufferWidth, bufferHeight)
// Setup frame buffer
var frameBuffer = GLuint()
glGenFramebuffers(GLsizei(10), &frameBuffer)
glBindFramebuffer(GLenum(GL_FRAMEBUFFER), frameBuffer)
glFramebufferRenderbuffer(GLenum(GL_FRAMEBUFFER), GLenum(GL_COLOR_ATTACHMENT0), GLenum(GL_RENDERBUFFER), renderBuffer)
// Draw
glReadBuffer(GLenum(GL_RENDERBUFFER))
glClearColor(0.1, 0.2, 0.3, 0.2)
glClear(GLbitfield(GL_COLOR_BUFFER_BIT))
//--------------
let bytes = malloc(width*height*4)
let bytes2 = malloc(width*height*4)
let x : GLint = GLint(0)
let y : GLint = GLint(0)
let w : GLsizei = GLsizei(width)
let h : GLsizei = GLsizei(height)
glReadPixels(x, y, w, h, GLenum(GL_RGBA), GLenum(GL_UNSIGNED_BYTE), bytes)
let data = NSData(bytes: bytes, length: width * height * 4)
let dataProvider = CGDataProvider(data: data)
//let dataProvider2 = CGDataProvider(dataInfo: nil, data: bytes!, size: width * height * 4, releaseData: )
let colorspace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo: CGBitmapInfo = [.byteOrder32Little, CGBitmapInfo(rawValue: CGImageAlphaInfo.last.rawValue)]
let aCGImage = CGImage(
width: Int(width),
height: Int(height),
bitsPerComponent: 8,
bitsPerPixel: 32,
bytesPerRow: 4 * Int(width),
space: colorspace,
bitmapInfo: bitmapInfo,
provider: dataProvider!,
decode: nil,
shouldInterpolate: true,
intent: .defaultIntent
)!
let imaag = UIImage(cgImage: aCGImage)
//I get the image of the same color that is defined at clearColor
现在我的问题是,我走的路对吗?我是否有可能以某种方式将我的 WKWebview(或拍摄它的快照并将其放入 UIView)到 renderBuffer/frameBuffer,以便 glReadPixels 实际读取屏幕上的内容?
附言!我已经看到很多关于从 CAEAGLView 中获取 UIImage 的问题,但在我的例子中,它不是 CAEGLView,而是 WKWebview。非常感激。
最佳答案
我有一个简单的方法来拍摄快照,有代码。
- (void)screenLoogShoot:(void (^)(UIImage *fullImage))snapShotHandler {
WKWebView *webView = self.webView;
UIScrollView *scrollView = webView.scrollView;
CGFloat boundsWidth = scrollView.bounds.size.width;
CGFloat contentHeight = scrollView.contentSize.height;
CGFloat scale = [UIScreen mainScreen].scale;
CGRect oldFrame = self.webView.frame;
if (@available(iOS 11.0, *)) {
self.webView.frame = CGRectMake(0, 0, boundsWidth, contentHeight);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
WKSnapshotConfiguration *configuration = [WKSnapshotConfiguration new];
configuration.rect = CGRectMake(0, 0, boundsWidth, contentHeight);
configuration.snapshotWidth = @(boundsWidth);
[self.webView takeSnapshotWithConfiguration:configuration completionHandler:^(UIImage * _Nullable snapshotImage, NSError * _Nullable error) {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(boundsWidth, contentHeight), NO, scale);
[snapshotImage drawInRect:CGRectMake(0, 0, boundsWidth, contentHeight)];
UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.webView.frame = oldFrame;
snapShotHandler(fullImage);
}];
});
} else {
self.webView.frame = CGRectMake(0, 0, boundsWidth, contentHeight);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(webView.bounds.size.width, webView.bounds.size.height), NO, scale);
[webView drawViewHierarchyInRect:CGRectMake(0, 0, boundsWidth, contentHeight) afterScreenUpdates:YES];
UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.webView.frame = oldFrame;
!snapShotHandler ? : snapShotHandler(fullImage);
});
}
}
关于ios - 使用硬件加速内容截取 WKWebview 的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45690932/
我想在我的 iPhone 应用程序中加入线性回归。经过一些搜索,我发现 Accelerate Framework 中的 LAPACK 和 BLAS 是正确的库。但是我很难将加速框架添加到我的 XCod
有什么方法可以加速 JS 脚本(我指的是一些复杂的 DOM 操作,比如游戏或动画)? 最佳答案 真的没有办法真正加快速度。您可以压缩它,但不会快很多。 关于Javascript 加速?,我们在Stac
有时,我必须为一个项目重新导入数据,从而将大约 360 万行读入 MySQL 表(目前是 InnoDB,但我实际上并不局限于这个引擎)。 “加载数据文件...”已被证明是最快的解决方案,但它有一个权衡
在尝试计算加速时,我被卡住了。所以给出的问题是: 问题 1 如果程序的 50% 增强了 2 倍,其余 50% 增强了 4 倍,那么由于增强而导致的整体加速是多少? Hints:考虑增强前(未增强)机器
目前我正在处理实时绘图,但可视化非常慢。我想知道你可以做些什么来加速 Matplotlib 中的事情: 后端如何影响性能?是否有后端 实时绘图比其他人更好吗? 我可以降低分辨率以提高 FPS 吗? 如
我有一个小型测试框架。它执行一个循环,执行以下操作: 生成一个小的 Haskell 源文件。 使用 runhaskell 执行此操作.该程序生成各种磁盘文件。 处理刚刚生成的磁盘文件。 这种情况发生了
这是我的网站:Instant-YouTube 如您所见,加载需要很长时间。在 IE8 及以下甚至有时会导致浏览器崩溃。我不确定是什么原因造成的。可能是 Clicksor 广告,但我认为是 swfobj
是否可以加速 SKSpriteNode? 我知道可以使用 node.physicsBody.velocity 轻松设置速度但是设置它的加速度有多难? 最佳答案 从牛顿第二定律倒推运动:F = m.a您
有没有人有加速 FCKEditor 的技术?是否有一些关键的 JavaScript 文件可以缩小或删除? 最佳答案 在最新版本 (3.0.1) 中,FCKEditor 已重命名为 CKEditor .
我有以下 MySQL 查询,需要一天多的时间才能执行: SELECT SN,NUMBER FROM a WHERE SN IN (SELECT LOWER_SN FROM b WHER
我现在正在开发一款使用加速来玩的游戏。我找到了如何让我的元素移动,但不改变它的“原点”,或者更准确地说,改变加速度计算的原点: 事实上,我的图像是移动的,它的中心是这样定义的: imageView.c
我有一个 mysql 表,其中存储有 4 列的成员消息: message_id(主键,自增) sender_id( key ) receiver_id( key ) 消息内容 我做了很多 SELECT
我在 cuda_computation.cu 中有以下代码 #include #include #include #include void checkCUDAError(const char
我正在使用 BeautifulSoup 在 for 循环中解析数千个网站。这是我的代码片段: def parse_decision(link): t1 = time.time() de
我正在使用 OpenCV 2.4 (C++) 在灰度图像上进行寻线。这涉及一些基本的图像处理步骤,如模糊、阈值、Canny 边缘检测器、梯度滤波器或霍夫变换。我必须在数千张图像上应用寻线算法。 考虑到
当我试图连续生成四次相同的报告时,我刚刚分析了我的报告应用程序。第一个用了 1859 毫秒,而后面的只用了 400 到 600 毫秒。对此的解释是什么?我能以某种方式使用它来使我的应用程序更快吗?报告
当我打开 Storyboard文件时,由于其中包含的 VC 数量,打开它需要 1-2 分钟。加快速度的最佳做法是什么?我们应该将一些 VC 移动到不同的 Storyboard文件中吗?我们是否应该使用
我有一个包含多个页面的 UIPageViewController。每个页面都是相同的 View Controller ,但会跟踪页码并显示 PDF 的正确页面。问题是每个 PDF 页面都需要在 cur
这实际上是两个问题,但它们非常相似,为了简单起见,我想将它们放在一起: 首先:给定一个已建立的 Java 项目,除了简单的代码内优化之外,还有哪些不错的方法可以加快它的速度? 其次:在用Java从头写
我有一个包含 1000 个条目的文档,其格式类似于:
我是一名优秀的程序员,十分优秀!