Different behavior UIImageView iPhone vs iPad

I have a piece of code that works on the iPhone. I load a UIImageView, make it invisible with an alpah = 0, and then play a video over it using AVPlayerLayer. When the video is done I remove the playerLayer (in a Notification) and change my alpha of myImage to 1.


Two different things happen when I use an iPad.

First, the alpha property does not do anything. The UIImage is visible.

Second, setting the playerLayer frame to the size of the myImage from only works when I use


[playerLayer setFrame:CGRectMake(0, 0, width, height)];


instead of just using


playerLayer.frame = myImage.frame


Here is the code. Thanks for anyone that can clear this up for me.


-(void)playMovie2: (NSString *) fileName

{

#define degreesToRadian(x) (M_PI*(x)/180.0)

CGFloat height, width;

NSLog(@"in playMovie2 simulation!");

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: fileName ofType:@"mov"]];

AVPlayer *player = [AVPlayer playerWithURL:url];

playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];

//cgrectmake: xfloat, yfloat, width, height

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){

height = self.view.frame.size.height;

width = self.view.frame.size.width;

[myImage setFrame:CGRectMake(0, 0, width, height)];

NSLog(@"iPhone %f %f",width, height);

}

else{

//we need this because I only used one XIB file for the iPhone and iPad

//we need to get the dimension from the device

height = [UIScreen mainScreen].currentMode.size.height;

width = [UIScreen mainScreen].currentMode.size.width;

height = (height/2);

width = (width/2);

[myImage setFrame:CGRectMake(0, 0, width, height)];

NSLog(@"iPad %f %f",width, height);

}

self.myImage.alpha = 0;

//[playerLayer setFrame:CGRectMake(0, 0, width, height)]; // For some reason I need to use this

playerLayer.frame = myImage.frame; //instead of this for iPad

[self.view.layer addSublayer:playerLayer];


[player play];

}

I seemed to get a good part working by adding the line of code:


self.myImage.frame = self.view.frame;


just above the [playerLayer addSublayer:playerLayer] line


What I don't understand is why it worked for the iPhone without this but not the iPad.

> adding the line of code: self.myImage.frame = self.view.frame; just above the [playerLayer addSublayer:playerLayer] line


Recheck what you posted to be sure there aren't additional lines missing.


1) what you wrote above does not include your height/2 correction - I don't understand that correction.

2) currentMode may be wrong. It has the following comment in the docs:

"The default value of this property is the mode containing the highest resolution supported by the screen."

that may make size.height differ from self.view.size.height depending on screen resolution

You are right. The currentMode was giving me twice the resolution. Using

width = [UIScreen mainScreen].bounds.size.width;

height = [UIScreen mainScreen].bounds.size.height;


gave me the correct width and height of the screen. I do not understand why "bounds" is the only option to get the size. I would have thought "frame" would be correct.

It's hard to follow your question or comment. Is your original question now answered by changing "currentMode" to "bounds"? If so, the reason is because currentMode was referring to a different resolution than the device you are using.


Are you now commenting that:

width = [UIScreen mainScreen].bounds.size.width;

height = [UIScreen mainScreen].bounds.size.height;

works but

width = [UIScreen mainScreen].frame.size.width;

height = [UIScreen mainScreen].frame.size.height;

does not?

Yes, I was commenting on your side comment, which was not the original question. My question about the screen sizing is that "frame" was not an option with [UIScreen mainScreen], yet I would think that I would use "frame" instead of "bounds".

Where are you setting the frame?

We have an issue with a client's app that on the iPad the screen size information isn't valid until view did appear.

The user is selecting different videos and the view is already loaded.

Different behavior UIImageView iPhone vs iPad
 
 
Q