Xcode 7 b5, Swift, GLKTextureLoader (Extra argument in call)

Hi,


GLKTextureLoader thrown an Error „Extra argument in call“, but I cannot see whats wrong.


var opt:[String : NSNumber] = [GLKTextureLoaderOriginBottomLeft : NSNumber(bool: false), GLKTextureLoaderApplyPremultiplication : NSNumber(bool: false)]
let pic = UIImage(named: "Car.png")
let tex:GLKTextureInfo?
do {
    try tex = GLKTextureLoader.textureWithCGImage(pic, options: opt)  // ERROR: Extra argument 'options' in call
} catch {
    tex = nil
}
//var tex = GLKTextureLoader.textureWithCGImage(cgImage: CGImage, options: [String : NSNumber]?)
tex = GLKTextureLoader.textureWithCGImage(cgImage: pic, options: opt)  // ERROR: Extra argument 'options' in call
tex = GLKTextureLoader.textureWithCGImage(pic, options: nil)           // ERROR: Extra argument 'options' in call
tex = GLKTextureLoader.textureWithCGImage(pic, nil)                    // ERROR: Extra argument in call


Can someone please tell me what I’m doing wrong here ?

Accepted Answer
var opt:[String : NSNumber] = [GLKTextureLoaderOriginBottomLeft : false, GLKTextureLoaderApplyPremultiplication : false]
let pic = UIImage(named: "Car.png")!.CGImage! //pic needs to be CGImage, not UIImage
let tex:GLKTextureInfo?
do {
    tex = try GLKTextureLoader.textureWithCGImage(pic, options: opt) //put `try` just before the method call
} catch {
    tex = nil
}

Destructing the expression into parts is a good strategy to find the issue, you just need to re-check the method signature.

+ textureWithCGImage:options:error:

class func textureWithCGImage(_

cgImage
: CGImage
,

options

options
: [String : NSNumber]?) throws -> GLKTextureInfo

Hi OOPer,


many thanks for answer !

Maybe you know where I can find a sample code ?

Maybe you know where I can find a sample code ?

In fact, I seached if I could find some working sample code, sorry, with no success. My reply above is just checked in the Playground seeing the reference.

What made me things easy was that I had experienced and once needed to fix putting `try` on top or confusing CGImage with UIImage.


ADDITION: This is the only sapmle code I could find in the Apple's developer library which is using GLKTextureLoader.

MusicCube: Classes/MusicCubeViewController.m

I'm not sure if it can be any help of you, as it uses another textureWithCGImage method.

The following code is the part using the textureWithCGImage translated into Swift.

        let image = UIImage(named: "speaker.png")!
        let textureloader = GLKTextureLoader(sharegroup: context.sharegroup)
        textureloader.textureWithCGImage(image.CGImage!, options: nil, queue: nil) {textureInfo, error in
           
            if error != nil {
                NSLog("Error loading texture %@",error!)
            } else {
                for f in 0..<6 {
                    self.cube[f].effect!.texture2d0.name = textureInfo!.name
                }
               
                self.cubeTexture = textureInfo!.name
            }
        }

Thanks for the info and your work, it was very helpful to me.

Thank you and have a nice day.

Xcode 7 b5, Swift, GLKTextureLoader (Extra argument in call)
 
 
Q