Swift3中的imageWithSize

时间:2016-09-25 17:36:01

标签: swift3 xcode8 unrecognized-selector

我正在将项目从Xcode 7 iOS 9 Swift 2转换为Xcode 8 iOS 10 Swift 3.我让Xcode转换为最新版本并生成:

 let artWork = delegate.musicPlayer.nowPlayingItem?.value(forProperty: MPMediaItemPropertyArtwork)
 let imageForButton = (artWork as AnyObject).image(at: CGSize(width: 300, height: 300))

此结果代码崩溃了应用程序并弹出错误:

- [_ SwiftValue imageWithSize:]:无法识别的选择器发送到实例0x17008cdf0 ***由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [_ SwiftValue imageWithSize:]:无法识别的选择器发送到实例

为了完整起见,Xcode 7 Swift 2中的原始行是:

let imageForButton = artWork?.imageWithSize(CGSizeMake(300, 300))

在Swift 3中调整图像大小的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

valueForProperty返回Any?,您需要将值转换为实际类型

if let artWork = delegate.musicPlayer.nowPlayingItem?.value(forProperty: MPMediaItemPropertyArtwork) as? MPMediaItemArtwork {
    let imageForButton = artWork.image(at: CGSize(width: 300, height: 300))
}