gpt4 book ai didi

ios - 拍摄 AGSMapView 的快照

转载 作者:行者123 更新时间:2023-11-30 13:21:21 26 4
gpt4 key购买 nike

使用以下代码拍摄 AGSMapView 的快照

此代码将设置 kEAGLDrawablePropertyRetainedBacking 属性,这是必需的,否则它将拍摄白色快照。

let layer =  self.AgsMapView.layer as! CAEAGLLayer
layer.drawableProperties = [
kEAGLDrawablePropertyRetainedBacking: NSNumber(int: 1), kEAGLDrawablePropertyColorFormat: kEAGLColorFormatRGBA8]

拍摄快照的代码

func snapshotForEAGLView() -> UIImage {

//glFlush()
var width: GLint = 0, height: GLint = 0
let scale: CGFloat = UIScreen.mainScreen().scale
let prevGLContext = EAGLContext.currentContext()
glGetRenderbufferParameteriv(GLenum(GL_RENDERBUFFER), GLenum(GL_RENDERBUFFER_WIDTH), &width)
glGetRenderbufferParameteriv(GLenum(GL_RENDERBUFFER), GLenum(GL_RENDERBUFFER_HEIGHT), &height)

let dataLength = width * height * 4
let imageBytes = UnsafeMutablePointer<GLubyte>.alloc(Int(dataLength))

glPixelStorei(GLenum(GL_PACK_ALIGNMENT), 4)

glReadPixels(0, 0, GLsizei(width), GLsizei(height), GLenum(GL_RGBA), GLenum(GL_UNSIGNED_BYTE), imageBytes)

let dataProvider = CGDataProviderCreateWithData(nil, imageBytes, Int(dataLength), nil)

EAGLContext.setCurrentContext(prevGLContext)

let colorSpace = CGColorSpaceCreateDeviceRGB()
if let imageRef = CGImageCreate(Int(width), Int(height), 8, 32, Int(width) * 4, colorSpace, [CGBitmapInfo.ByteOrder32Big, CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)], dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault) {
return UIImage(CGImage: imageRef, scale: scale, orientation: .DownMirrored)
}
return UIImage()
}

在应用程序中,我们在两个 Controller 上有 AGSMapView,比如说 Controller1Controller2,点击 Controller1 上的按钮code>,Controller2 被推送。

我们正在 Controller2 上截屏,但有时它会从 Controller1 截取 AGSMapView 的屏幕截图

它是随机发生的,我们是否可以提供要截取屏幕截图的 View 或任何其他方法来避免这种情况。

非常感谢任何帮助。

最佳答案

drawViewHierarchyInRect

成功了,谢谢大家

    let size = CGSizeMake(viewWidth, viewHeight)

UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.mainScreen().scale)

let rec = CGRectMake(0, 0, viewWidth, viewHeight)
self.agsMapView.drawViewHierarchyInRect(rec, afterScreenUpdates: true)

let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;

关于ios - 拍摄 AGSMapView 的快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37742350/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com