快速扩展的UIImage init表示" Extra Argument"误

时间:2015-03-26 13:27:19

标签: ios objective-c swift uiimage

行。这很糟糕。我已经检查了上面的各种答案,this is the only one that seems to really hit the mark

但是,似乎没有答案。

我正在使用Swift创建一个新的UIImage,就像这样(Objective-C版本):

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

工作正常。但是,当我在Swift中尝试the same exact call时:

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

我收到编译错误,告诉我" scale"是一个额外的参数。

这是签名错误时会出现的错误。

然而,这并没有错(据我所知)。

我正在做的是通过在Swift 中完全复制Objective-C函数来创建教程(我知道,我知道,Swift是不同的,所以我不应该完全复制,但是,作为Bill默里会说,"宝贝步骤")

我想知道我是怎么搞砸的。

如果我只用图像调用它(没有比例或方向),它可以正常工作,但我需要那个比例。

2 个答案:

答案 0 :(得分:1)

我在游乐场尝试了这个,并且能够编译这段代码:

import UIKit
import AVFoundation

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

此语法也有效:

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

答案 1 :(得分:0)

行。我把它弄清楚了。我是一个傻瓜。

我需要在CGImage上使用takeRetainedValue(),如下所示:

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

满足签名。