Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.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 Swift 3中将openGL ES图形保存为PNG图像文件_Ios_Swift_Opengl Es_Swift3_Uiimage - Fatal编程技术网

在iOS Swift 3中将openGL ES图形保存为PNG图像文件

在iOS Swift 3中将openGL ES图形保存为PNG图像文件,ios,swift,opengl-es,swift3,uiimage,Ios,Swift,Opengl Es,Swift3,Uiimage,我正在尝试使用CGDataProvider和CGImage函数将使用openGL ES在iOS上生成的图形保存到PNG文件。我在网上找到了一些Objective-C代码,并将其转换为Swift 3。代码进行编译,但在运行时失败并引发错误: 致命错误:在此行展开可选值时意外发现nil: let iref: CGImage = CGImage(pngDataProviderSource: provider!, decode: nil, shouldInterpolate: true, intent:

我正在尝试使用CGDataProvider和CGImage函数将使用openGL ES在iOS上生成的图形保存到PNG文件。我在网上找到了一些Objective-C代码,并将其转换为Swift 3。代码进行编译,但在运行时失败并引发错误: 致命错误:在此行展开可选值时意外发现nil

let iref: CGImage = CGImage(pngDataProviderSource: provider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent)!
代码:

设x:Int=0
设y:Int=0
让数据长度:Int=Int(宽)*Int(高)*4
let像素:不可女性化的RawPointer?=malloc(数据长度*MemoryLayout.size)
glPixelStorei(GL_PACK_ALIGNMENT.ui,4)
glReadPixels(闪烁(x)、闪烁(y)、GLsizei(宽度)、GLsizei(高度)、GLenum(GL_RGBA)、GL_UNSIGNED_BYTE.ui、像素)
让pixelData:UnsafePointer=(UnsafeRawPointer(像素)?.AssumingMemoryBind(to:UInt8.self))!
让cfdata:cfdata=CFDataCreate(kcfolocatordefault,pixelData,dataLength*MemoryLayout.size)
让提供程序:CGDataProvider!=CGDataProvider(数据:cfdata)
让iref:CGImage=CGImage(pngDataProviderSource:provider!,decode:nil,shouldInterpolate:true,intent:CGColorRenderingIntent.defaultIntent)!
UIGraphicsBeginImageContext(CGSize(宽度:CGFloat(宽度),高度:CGFloat(高度)))
让cgcontext:cgcontext?=UIGraphicsGetCurrentContext()
cgcontext!。setBlendMode(CGBlendMode.copy)
cgcontext!。绘制(iref,in:CGRect(x:CGFloat(0.0),y:CGFloat(0.0),宽度:CGFloat(宽度),高度:CGFloat(高度)))
让图像:UIImage?=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()

我在网上找不到有关Swift 3的任何帮助。请您帮助修复此错误。

正如Matic Oblak所建议的,当我在上面的代码中替换这一行时,错误消失了,代码工作正常

let iref: CGImage = CGImage(pngDataProviderSource: provider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent)!
用这个

let iref: CGImage? = 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: true, intent: CGColorRenderingIntent.defaultIntent)
新工作守则:

let x: Int = 0
let y: Int = 0
let dataLength: Int = Int(width) * Int(height) * 4
let pixels: UnsafeMutableRawPointer? = malloc(dataLength * MemoryLayout<GLubyte>.size)
glPixelStorei(GL_PACK_ALIGNMENT.ui, 4)
glReadPixels(GLint(x), GLint(y), GLsizei(width), GLsizei(height), GLenum(GL_RGBA), GL_UNSIGNED_BYTE.ui, pixels)
let pixelData: UnsafePointer = (UnsafeRawPointer(pixels)?.assumingMemoryBound(to: UInt8.self))!
let cfdata: CFData = CFDataCreate(kCFAllocatorDefault, pixelData, dataLength * MemoryLayout<GLubyte>.size)

let provider: CGDataProvider! = CGDataProvider(data: cfdata)

let iref: CGImage? = 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: true, intent: CGColorRenderingIntent.defaultIntent)
UIGraphicsBeginImageContext(CGSize(width: CGFloat(width), height: CGFloat(height)))
let cgcontext: 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()
设x:Int=0
设y:Int=0
让数据长度:Int=Int(宽)*Int(高)*4
let像素:不可女性化的RawPointer?=malloc(数据长度*MemoryLayout.size)
glPixelStorei(GL_PACK_ALIGNMENT.ui,4)
glReadPixels(闪烁(x)、闪烁(y)、GLsizei(宽度)、GLsizei(高度)、GLenum(GL_RGBA)、GL_UNSIGNED_BYTE.ui、像素)
让pixelData:UnsafePointer=(UnsafeRawPointer(像素)?.AssumingMemoryBind(to:UInt8.self))!
让cfdata:cfdata=CFDataCreate(kcfolocatordefault,pixelData,dataLength*MemoryLayout.size)
让提供程序:CGDataProvider!=CGDataProvider(数据:cfdata)
让iref:CGImage?=CGImage(宽度:Int(宽度),高度:Int(高度),bitsPerComponent:8,bitsPerPixel:32,bytesPerRow:Int(宽度)*4,空格:colorspace!,bitmapInfo:CGBitmapInfo.byteOrder32Big,提供程序:提供程序,解码:nil,应中间值:true,意图:CGColorRenderingIntent.defaultIntent)
UIGraphicsBeginImageContext(CGSize(宽度:CGFloat(宽度),高度:CGFloat(高度)))
让cgcontext:cgcontext?=UIGraphicsGetCurrentContext()
cgcontext!。setBlendMode(CGBlendMode.copy)
cgcontext!。绘制(iref,in:CGRect(x:CGFloat(0.0),y:CGFloat(0.0),宽度:CGFloat(宽度),高度:CGFloat(高度)))
让图像:UIImage?=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()

至少检查哪一个为零。使用2个力展开。一个是提供程序,另一个是整个CGImage。无论如何,请尝试为提供程序使用不同的构造函数:CGDataProvider(dataInfo:nil data:cfdata,size:size,releaseData:nil)整个CGImage将变为零。我尝试了您提供的构造函数,但失败了,错误消息是:Nil与预期的参数类型“CGDataProviderReleaseDataCallback”(又名“@convention(c)(可选,UnsafeRawPointer,Int)->()”)不兼容。让provider=CGDataProvider(dataInfo:Nil data:cfdata,size:size,releaseData:Nil)啊,我现在明白了。您正在CGImage上使用构造函数,该构造函数需要原始PNG数据,但您有原始RGBA数据。您使用的方法需要(例如)来自PNG图像文件内容的数据。您需要对原始RGBA数据使用完整构造函数:CGImage(宽度:,高度:,bitsPerComponent:,bitsPerPixel:,bytesPerRow:,空格:,bitmapInfo:,提供程序:,解码:,应插入:,意图:)@Matic Oblak,非常感谢。这就解决了问题为了便于将来参考,您可能需要在答案中编写生成的代码。
let x: Int = 0
let y: Int = 0
let dataLength: Int = Int(width) * Int(height) * 4
let pixels: UnsafeMutableRawPointer? = malloc(dataLength * MemoryLayout<GLubyte>.size)
glPixelStorei(GL_PACK_ALIGNMENT.ui, 4)
glReadPixels(GLint(x), GLint(y), GLsizei(width), GLsizei(height), GLenum(GL_RGBA), GL_UNSIGNED_BYTE.ui, pixels)
let pixelData: UnsafePointer = (UnsafeRawPointer(pixels)?.assumingMemoryBound(to: UInt8.self))!
let cfdata: CFData = CFDataCreate(kCFAllocatorDefault, pixelData, dataLength * MemoryLayout<GLubyte>.size)

let provider: CGDataProvider! = CGDataProvider(data: cfdata)

let iref: CGImage? = 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: true, intent: CGColorRenderingIntent.defaultIntent)
UIGraphicsBeginImageContext(CGSize(width: CGFloat(width), height: CGFloat(height)))
let cgcontext: 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()