Search code examples
iosswiftmpmediaitem

Cannot set Album Artwork as background color


I have pulled the image of album artwork of a selected song using MPMediaItemArtwork and I am attempting to set this artwork as the image background similarly to how spotify has this set up, however I keep getting two errors

Cannot assign a value of type 'UIColor' to a type of value 'UIColor?'

as well as

Could not find an overload for 'init' that accepts the supplied arguments

And here is my code below

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()
    }

}

I know I probably have some stupid mistake but I cannot figure it out


Solution

  • You're creating UIImage by calling UIImage(named:) which expects string and you're passing in a MPMediaItemArtwork instance. This is gonna compile:

    let albumArtwork = MPMediaItemArtwork()
    let size = CGSize(width: 250, height: 250)
    let image = albumArtwork.imageWithSize(size)
    self.view.backgroundColor = UIColor(patternImage: image!)
    

    Also notice that you set the background color and then change it few lines after, so the image will not appear:

    self.view.backgroundColor = UIColor(patternImage: image!)
    if !UIAccessibilityIsReduceTransparencyEnabled() {
        self.view.backgroundColor = UIColor.clearColor()
        // ... some more code ....
    } else {
        self.view.backgroundColor = UIColor.whiteColor()
    }