MPMediaItemArtwork init(image:) deprecated in iOS 10.0

后端 未结 4 993
执笔经年
执笔经年 2021-02-13 16:35

Apple has deprecated init(image:) method in MPMediaItemArtwork in iOS 10. What is the new alternative.

the class shows interface shows method

相关标签:
4条回答
  • 2021-02-13 16:58

    Minimum code:

    MPMediaItemArtwork(boundsSize: image.size) { _ in image }
    
    0 讨论(0)
  • 2021-02-13 17:03

    I was wondering the same and ended up finding Apple's explanation for this.

    They say we shouldn't do any expensive resizing operations on the image when handler is requested, but instead simply return the closely matching image out of ones already available to you.

    The following WWDC 2017 video is where they mention it. It's about tvOS, but at least we get some insight. Starts at 07:20: https://developer.apple.com/videos/play/wwdc2017/251/?time=440

    0 讨论(0)
  • 2021-02-13 17:07

    You can use the following code:

    let image = UIImage(named: "logo")!
    let artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in
            return image
    })
    

    And, yes, "now playing" metadata shows on the control center in the simulator.

    0 讨论(0)
  • 2021-02-13 17:14

    I saw this just now and I'm confused too, but I guess this is the right way:

    self.remoteArtwork = [[MPMediaItemArtwork alloc] initWithBoundsSize:CGSizeMake(600, 600) requestHandler:^UIImage * _Nonnull(CGSize size) {
    
        UIImage *lockScreenArtworkApp = [UIImage imageNamed:@"lockScreenLogo"];
    
        return [self.manager resizeImageWithImage:lockScreenArtworkApp scaledToSize:size];        
    }];
    

    The method - in my case in a singleton "Manager"-Class

    - (UIImage *)resizeImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
        //UIGraphicsBeginImageContext(newSize);
        // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
        // Pass 1.0 to force exact pixel size.
        UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
        [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    
    0 讨论(0)
提交回复
热议问题