我使用MPMediaItemArtwork
拉取了所选歌曲的album artwork
image
,我正在尝试将此艺术品设置为图像background
类似于spotify
设置的方式,但是我不断收到两个错误
无法将"UIColor"类型的值分配给值"UIColor"的类型?
以及
找不到接受提供的"init"的重载 参数
这是我下面的代码
func setAlbumCover(){
var AlbumImage = MPMediaItemArtwork()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: AlbumImage)!)
if !UIAccessibilityIsReduceTransparencyEnabled() {
self.view.backgroundColor = UIColor.clearColor()
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.ExtraLight)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.view.bounds
self.view.addSubview(blurEffectView) //if you have more UIViews on screen, use insertSubview:belowSubview: to place it underneath the lowest view instead
//add auto layout constraints so that the blur fills the screen upon rotating device
blurEffectView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addConstraint(NSLayoutConstraint(item: blurEffectView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: blurEffectView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: blurEffectView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: blurEffectView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
} else {
self.view.backgroundColor = UIColor.whiteColor()
}
}
我知道我可能有一些愚蠢的错误,但我无法弄清楚
您通过调用需要字符串的UIImage(named:)
来创建UIImage
,并且您正在传入MPMediaItemArtwork
实例。这将编译:
let albumArtwork = MPMediaItemArtwork()
let size = CGSize(width: 250, height: 250)
let image = albumArtwork.imageWithSize(size)
self.view.backgroundColor = UIColor(patternImage: image!)
另请注意,您设置了背景颜色,然后在几行之后更改了它,因此图像将不会显示:
self.view.backgroundColor = UIColor(patternImage: image!)
if !UIAccessibilityIsReduceTransparencyEnabled() {
self.view.backgroundColor = UIColor.clearColor()
// ... some more code ....
} else {
self.view.backgroundColor = UIColor.whiteColor()
}