【问题标题】:Cannot invoke initializer for type 'CGBitmapInfo' with an argument list of type '(UInt32)'无法使用类型为“(UInt32)”的参数列表调用类型“CGBitmapInfo”的初始化程序
【发布时间】:2016-02-26 10:03:49
【问题描述】:

我将图像作为纹理添加到金属球体。使用 swift 2.0,但我收到一个错误 CGBitmapInfo(options),它说:无法使用类型为“(UInt32)”的参数列表调用类型为“CGBitmapInfo”的初始化程序......有人知道如何解决吗?这是我的代码:

func textureForImage(image:UIImage, device:MTLDevice) -> MTLTexture?
{
    let imageRef = image.CGImage

    let width = CGImageGetWidth(imageRef)
    let height = CGImageGetHeight(imageRef)
    let colorSpace = CGColorSpaceCreateDeviceRGB()

    let rawData = calloc(height * width * 4, sizeof(UInt8))

    let bytesPerPixel = 4
    let bytesPerRow = bytesPerPixel * width
    let bitsPerComponent = 8

    let options = CGImageAlphaInfo.PremultipliedLast.rawValue | CGBitmapInfo.ByteOrder32Big.rawValue

    let context = CGBitmapContextCreate(rawData,
                                        width,
                                        height,
                                        bitsPerComponent,
                                        bytesPerRow,
                                        colorSpace,
                                        CGBitmapInfo(options))


    CGContextDrawImage(context, CGRectMake(0, 0, CGFloat(width), CGFloat(height)), imageRef)

    let textureDescriptor = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(.RGBA8Unorm,
                                                                                    width: Int(width),
                                                                                    height: Int(height),
                                                                                    mipmapped: true)
    let texture = device.newTextureWithDescriptor(textureDescriptor)

    let region = MTLRegionMake2D(0, 0, Int(width), Int(height))

    texture.replaceRegion(region,
                          mipmapLevel: 0,
                          slice: 0,
                          withBytes: rawData,
                          bytesPerRow: bytesPerRow,
                          bytesPerImage: bytesPerRow * height)

    free(rawData)

    return texture
}

【问题讨论】:

标签: ios swift2 metal


【解决方案1】:

在当前版本的 Swift 中,CGBitmapContextCreate 的最终参数是UInt32,所以代替上面的CGBitmapInfo(options),你应该只传递options(分配给options 的表达式被推断为有类型UInt32)。

【讨论】:

  • 谢谢!我很高兴它受到了好评。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多