我想在GLKView中显示CIImage。 GLKView位于包含视图(超级视图)中。我希望GLKView将图像呈现为长宽比填充。我希望GLKView占用包含UIView的所有空间。
但是不管我似乎做什么,都会以错误的方式出现。结果可以在下图中看到。包含的视图具有红色背景,使其易于查看。顶部是导航栏,底部是带有一些灰色图标的工具栏。
我根本不知道为什么GLKView不会填满所有包含的视图。
这是执行此操作的GLRenderer的代码
class GLImageRenderer {
private var glContext: EAGLContext
private var videoDisplayViewBounds: CGRect
private var videoDisplayView: GLKView
private var scale: CGFloat
private var renderContext: CIContext
init(superView: UIView) {
superView.backgroundColor = UIColor.red
glContext = EAGLContext(api: EAGLRenderingAPI.openGLES2)!
videoDisplayView = GLKView(frame: superView.frame, context: glContext)
videoDisplayView.contentScaleFactor = UIScreen.main.scale
videoDisplayView.contentMode = .scaleAspectFill
videoDisplayView.frame = superView.frame
videoDisplayView.transform = CGAffineTransform.init(rotationAngle: CGFloat.pi/2)
superView.addSubview(videoDisplayView)
superView.sendSubviewToBack(videoDisplayView)
renderContext = CIContext(eaglContext: videoDisplayView.context)
videoDisplayView.bindDrawable()
videoDisplayViewBounds = CGRect(x: 0,
y: 0,
width: videoDisplayView.drawableWidth,
height: videoDisplayView.drawableHeight)
scale = UIScreen.main.scale
}
func draw(ciimage: CIImage) {
let drawFrame = ciimage.extent
let imageAspectRatio = drawFrame.width / drawFrame.height
let viewAspectRatio = videoDisplayViewBounds.width / videoDisplayViewBounds.height
var newDrawFrame: CGRect
if imageAspectRatio > viewAspectRatio {
newDrawFrame = CGRect(x: drawFrame.minX + ((drawFrame.width - drawFrame.height * viewAspectRatio) / scale),
y: drawFrame.minY,
width: drawFrame.height * viewAspectRatio,
height: drawFrame.height)
} else {
newDrawFrame = CGRect(x: drawFrame.minX,
y: drawFrame.minY + ((drawFrame.height - drawFrame.width / viewAspectRatio) / scale),
width: drawFrame.width,
height: drawFrame.width / viewAspectRatio)
}
videoDisplayView.bindDrawable()
if videoDisplayView.context != EAGLContext.current() {
EAGLContext.setCurrent(videoDisplayView.context)
}
glClearColor(0.5, 0.5, 0.5, 1.0)
glClear(GLbitfield(GL_COLOR_BUFFER_BIT))
glEnable(GLenum(GL_BLEND))
glBlendFunc(GLenum(GL_ONE), GLenum(GL_ONE_MINUS_SRC_ALPHA))
renderContext.draw(ciimage, in:videoDisplayViewBounds, from: newDrawFrame)
videoDisplayView.display()
}
func close() {
videoDisplayView.removeFromSuperview()
}
}
可以看出,我将GLKView的内容模式设置为宽高比,并且GLKView的帧设置为与超级视图匹配,但是在渲染时仍然不匹配。我还尝试了ciimage draw方法的CGRect参数的不同组合,尽管我显然得到了一些不同的结果,但它们都都在同一位置并且永远不会填满超级视图。
有什么想法吗?