Swift3 中的 imageWithSize [英] imageWithSize in Swift3

查看:33
本文介绍了Swift3 中的 imageWithSize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am converting a project from Xcode 7 iOS 9 Swift 2 to Xcode 8 iOS 10 Swift 3. I let Xcode convert to the latest and it produced:

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

由此产生的代码使应用程序崩溃并弹出错误:

This resulting code crashes the app and pops the error:

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

-[_SwiftValue imageWithSize:]: unrecognized selector sent to instance 0x17008cdf0 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue imageWithSize:]: unrecognized selector sent to instance

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

For completeness, the original line in Xcode 7 Swift 2 was:

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

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

What is the correct way to size an image in Swift 3?

推荐答案

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

valueForProperty returns Any?, you need to cast the value to the actual type

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

这篇关于Swift3 中的 imageWithSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆