我正在尝试创建一个 UIImageView,其中包含当前在音乐播放器上播放的歌曲的专辑插图。我找到的所有资源要么在 objective-c(我不知道)中,要么不起作用(也许我没有正确实现它)。
我目前正在尝试使用这段代码来做到这一点:
class ViewController: UIViewController {
@IBOutlet weak var backgroundAlbum: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
backgroundAlbum.image = MPMediaItemArtwork()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
但是我在行中遇到错误
backgroundAlbum.image = MPMediaItemArtwork()
最佳答案
试试这个:
//this method is called after you picked a song from music library
func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
dismissViewControllerAnimated(true, completion: nil)
//get current song
let currentSong: MPMediaItem = mediaItemCollection.items[0]
if let artwork: MPMediaItemArtwork = currentSong.valueForProperty(MPMediaItemPropertyArtwork) as? MPMediaItemArtwork{
albumArt.image = artwork.imageWithSize(CGSize(width: x, height: y))
}
关于ios - 如何使用 swift 获取当前播放音乐的专辑封面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28979013/