Click here to Skip to main content
13,448,170 members (54,110 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


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.


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.


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

        [info setObject:[[MPMediaItemArtwork alloc] initWithImage:newImage] 

    [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.


  • 12/8/2016: Initial version


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


About the Author

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...


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
Web01-2016 | 2.8.180318.3 | Last Updated 8 Dec 2016
Article Copyright 2016 by KarstenK
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid