Click here to Skip to main content
13,150,607 members (37,241 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8.9K views
3 bookmarked
Posted 8 Dec 2016

How to Fine Tune the Multimedia Controls

, 8 Dec 2016
Rate this:
Please Sign up or sign in to vote.
Show correct image in the now-playing information with the MPNowPlayingInfoCenter API.

Introduction

For any media app, it is needed to bring some information about the playback with a correctly scaled image in the lock screen of iOS devices to enrich the user experience.

Background

I needed this feature, but didn't find a clean solution on the internet. So I figured it out and now want to share my knowledge.

Using the Code

It is the straight use of the MPNowPlayingInfoCenter API from Apple with the trick to get the image centered and scaled into a quadratic taget image.

- (void)refreshNowPlayingInfo:(UIImage*) image
{
    NSMutableDictionary *info = [NSMutableDictionary dictionary];
    // add some information
    [info setObject:@"Artist" forKey:MPMediaItemPropertyArtist];

    if (image) {
		const CGFloat SIZE = 128*8;//could be other value
		CGSize size = image.size;
		CGFloat x = size.width;
		CGFloat y = size.height;
        //ensure proper scaling
		if( x > y )	{
			y = (y * SIZE) / x;
			x = SIZE;
		} else if( x < y )
		{
			x = (x * SIZE) / y;
			y = SIZE;
		} else {
			x = SIZE;
			y = SIZE;
		}
        //create a new image
		UIGraphicsBeginImageContext( CGSizeMake(SIZE, SIZE) );//full size
        //draw so, that it got centered in x and y
		[image drawInRect:CGRectMake( (SIZE-x)/2, (SIZE-y)/2, x, y)];
		UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
		UIGraphicsEndImageContext();

        [info setObject:[[MPMediaItemArtwork alloc] initWithImage:newImage] 
                        forKey:MPMediaItemPropertyArtwork];
    }

    [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = info;
}

Points of Interest

Interesting to see how easy the MPNowPlayingInfoCenter API works, but why do I have to center and scale the picture myself. For further information, Apple has provided the MPNowPlayingInfoCenter documentation.

History

  • 12/8/2016: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

KarstenK
Software Developer
Germany Germany
I am living in germany and now living from programming for some Years. In my spare time I like sports as jogging, playing football (soccer) and basketball.

We must take care for our planet, because we and our family has no other. And everybody has to do something for it.

You may also be interested in...

Pro

Comments and Discussions

 
PraiseIt works! Pin
Andre Jones29-Dec-16 6:36
memberAndre Jones29-Dec-16 6:36 
PraisePerfect Code Pin
JanBask Training13-Dec-16 0:17
memberJanBask Training13-Dec-16 0:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170924.2 | Last Updated 8 Dec 2016
Article Copyright 2016 by KarstenK
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid