Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios glReadPixels到UIImage(),黑色图像_Ios_Swift_Opengl Es - Fatal编程技术网

Ios glReadPixels到UIImage(),黑色图像

Ios glReadPixels到UIImage(),黑色图像,ios,swift,opengl-es,Ios,Swift,Opengl Es,我正在尝试拍摄vuforia相机的屏幕截图。我正在使用这个代码。它在iPhone7(ios 11.3)和iPadPro(ios 11.2)上的完美运行。但这段代码在iPad2(iOS9.3.5)上不起作用,函数返回有效的UIImage,但它是黑色的 static public func takeScreenshot() -> UIImage? { let xCoord: Int = 0 let yCoord: Int = 0 let screen = UIS

我正在尝试拍摄vuforia相机的屏幕截图。我正在使用这个代码。它在iPhone7(ios 11.3)和iPadPro(ios 11.2)上的完美运行。但这段代码在iPad2(iOS9.3.5)上不起作用,函数返回有效的UIImage,但它是黑色的

    static public func takeScreenshot() -> UIImage? {
    let xCoord: Int = 0
    let yCoord: Int = 0
    let screen = UIScreen.main.bounds

    let scale = UIScreen.main.scale
    let width = screen.width * scale
    let height = screen.height * scale
    let dataLength: Int = Int(width) * Int(height) * 4
    let pixels: UnsafeMutableRawPointer? = malloc(dataLength * MemoryLayout<GLubyte>.size)
    glPixelStorei(GLenum(GL_PACK_ALIGNMENT), 4)
    glReadPixels(GLint(xCoord), GLint(yCoord), GLsizei(width), GLsizei(height), GLenum(GL_RGBA), GLenum(GL_UNSIGNED_BYTE), pixels)
    guard let pixelData: UnsafePointer = (UnsafeRawPointer(pixels)?.assumingMemoryBound(to: UInt8.self)) else { return nil }
    let cfdata: CFData = CFDataCreate(kCFAllocatorDefault, pixelData, dataLength * MemoryLayout<GLubyte>.size)

    let provider: CGDataProvider! = CGDataProvider(data: cfdata)
    let colorspace = CGColorSpaceCreateDeviceRGB()
    guard let iref = CGImage(width: Int(width),
                                 height: Int(height),
                                 bitsPerComponent: 8,
                                 bitsPerPixel: 32,
                                 bytesPerRow: Int(width)*4,
                                 space: colorspace,
                                 bitmapInfo: CGBitmapInfo.byteOrder32Big,
                                 provider: provider,
                                 decode: nil,
                                 shouldInterpolate: false,
                                 intent: CGColorRenderingIntent.defaultIntent) else { return nil }
    UIGraphicsBeginImageContext(CGSize(width: CGFloat(width), height: CGFloat(height)))
    if let cgcontext = UIGraphicsGetCurrentContext() {
        cgcontext.setBlendMode(CGBlendMode.copy)
        cgcontext.draw(iref, in: CGRect(x: CGFloat(0.0), y: CGFloat(0.0), width: CGFloat(width), height: CGFloat(height)))
        let image: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    return nil
}
static public func takeScreenshot()->UIImage?{
设xCoord:Int=0
让yCoord:Int=0
let screen=UIScreen.main.bounds
让scale=UIScreen.main.scale
让宽度=屏幕宽度*比例
让高度=屏幕高度*比例
让数据长度:Int=Int(宽)*Int(高)*4
let像素:UnsafemeutableRawPointer?=malloc(数据长度*MemoryLayout.size)
glPixelStorei(GLenum(Glu-PACK-Arrignment),4)
glReadPixels(闪烁(xCoord)、闪烁(yCoord)、GLsizei(宽度)、GLsizei(高度)、GLenum(GL_RGBA)、GLenum(GL_无符号字节)、像素)
guard let pixelData:UnsafePointer=(UnsafeRawPointer(像素)?.AssumingMemoryBind(to:UInt8.self))else{return nil}
让cfdata:cfdata=CFDataCreate(kcfolocatordefault,pixelData,dataLength*MemoryLayout.size)
let provider:CGDataProvider!=CGDataProvider(数据:cfdata)
让colorspace=CGColorSpaceCreateDeviceRGB()
保护线iref=CGImage(宽度:Int(宽度),
高度:Int(高度),
比特组件:8,
比特像素:32,
bytesPerRow:Int(宽度)*4,
空间:色彩空间,
bitmapInfo:CGBitmapInfo.byteOrder32Big,
提供者:提供者,
解码:无,
应该中间:错,
intent:CGColorRenderingIntent.defaultIntent)else{return nil}
UIGraphicsBeginImageContext(CGSize(宽度:CGFloat(宽度),高度:CGFloat(高度)))
如果让cgcontext=UIGraphicsGetCurrentContext(){
cgcontext.setBlendMode(CGBlendMode.copy)
绘制(iref,in:CGRect(x:CGFloat(0.0),y:CGFloat(0.0),width:CGFloat(width),height:CGFloat(height)))
let image:UIImage?=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
返回图像
}
归零
}

更新:我解决了这个问题,需要在opengl线程中运行函数,opengl ES接受的格式非常有限。有一个很好的网站,上面有OpenGL文档

您对或感兴趣。缓冲区格式应为GL_RGBA或GL_BGRA

也许更好的方法是