【问题标题】:imageWithSize in Swift3Swift3 中的 imageWithSize
【发布时间】:2016-09-25 17:36:01
【问题描述】:

我正在将一个项目从 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 中调整图像大小的正确方法是什么?

【问题讨论】:

    标签: swift3 xcode8 unrecognized-selector


    【解决方案1】:

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

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

    【讨论】:

    • 我必须使用'as!'而不是“作为?”但这解决了它。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多