1

OK. This is whacky. I've checked the various answers above, and this is the only one that seems to really hit the mark.

However, there doesn't seem to be an answer.

I am using Swift to create a new UIImage, exactly like this (Objective-C version):

UIImage *myImage = [[UIImage alloc] initWithCGImage: aCGImageRefAllocatedPreviously scale:aCGFloatScaleCalculatedPreviously orientation:UIImageOrientationUp];

That works fine. However, when I try the same exact call in Swift:

let myImage = UIImage ( CGImage: aCGImageAllocatedPreviously, scale:aCGFloatScaleCalculatedPreviously, orientation:UIImageOrientation.Up )

I get a compiler error, telling me that "scale" is an extra parameter.

This is the kind of error that you get when the signature is wrong.

However, it isn't wrong (as far as I can tell).

What I am doing, is creating a tutorial by exactly replicating an Objective-C function in Swift (I know, I know, Swift is different, so I shouldn't exactly replicate, but, as Bill Murray would say, "Baby Steps").

I'm wondering how the heck I'm screwing up.

If I call it with just the image (no scale or orientation), it works fine, but I need that scale.

Community
  • 1
  • 1
Chris Marshall
  • 4,910
  • 8
  • 47
  • 72

2 Answers2

1

I tried this in the playground and was able to get this code to compile:

import UIKit
import AVFoundation

let myImage = UIImage(CGImage: nil,
  scale:0,
  orientation:UIImageOrientation.Up)

This syntax also works:

let newImage = UIImage.init(CGImage: nil,
  scale:0,
  orientation:UIImageOrientation.Up )
Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

OK. I figgered it out. I was a knucklehead.

I need to use takeRetainedValue() on the CGImage, like so:

let myImage = UIImage ( CGImage: aCGImageAllocatedPreviously.takeRetainedValue(),scale:aCGFloatScaleCalculatedPreviously,orientation:UIImageOrientation.Up )

That satisfies the signature.

Chris Marshall
  • 4,910
  • 8
  • 47
  • 72
  • 1
    Is it takeRetainedImage() or takeRetainedValue()? – I assume the latter. Please do copy/paste the real code in your question and answers. Otherwise you are wasting your time and that of the readers. – Martin R Mar 26 '15 at 13:49
  • @Martin R Good point. The problem is that I am working on a work machine that has no internet connection, and turning around and working on this laptop, so stuff is getting lost in translation. I'll fix it. No, it's not wasting time. It's called "legitimate error," and we all do it. I appreciate it being pointed out. – Chris Marshall Mar 26 '15 at 15:13