拍摄AGSMapView的快照

时间:2016-06-10 07:27:21

标签: ios objective-c swift arcgis

使用以下代码拍摄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()
    }

在应用中,我们在两个控制器上AGSMapView,让我们说Controller1Controller2,点击Controller1上的按钮,Controller2被推送。

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

它是随机出现的,有什么方法可以让我们提供哪种视图来截取屏幕截图或任何其他方式来避免这种情况。

非常感谢任何帮助。非常感谢。

1 个答案:

答案 0 :(得分:0)

  

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;