2016-02-26 88 views
0

我將圖像作爲紋理添加到金屬球體中。使用swift 2.0,但我得到一個錯誤CGBitmapInfo(選項),它說:不能用類型'(UInt32)'的參數列表調用類型'CGBitmapInfo'的初始值設定項...任何人都知道如何解決它?這裏是我的代碼:無法使用類型爲'(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 
} 
+1

檢查了這一點 - http://stackoverflow.com/questions/24109149/cgbitmapcontextcreate-error-with-swift – Shripada

+0

好〜謝謝你... – Eleanor

回答

2

在斯威夫特的當前版本中,CGBitmapContextCreate的最後一個參數是一個UInt32,所以代替上述CGBitmapInfo(options),你應該只通過options(分配給options表達推斷有類型UInt32)。

+0

謝謝~~~修正它~~~順便說一句,酷教程關於金屬~~~ – Eleanor

+0

謝謝!我很高興它受到好評。 – warrenm

相關問題