Trying to break down a problem that I had for a while now
Background
Let's assume I try to calculate a UIImage that should fit perfectly into a UIImageView, that is layouted with constraints in the storyboard.
The content of the imageView should not be filled / aspect fitted ... but fit perfectly into the frame. Therefore i have a method in my viewController
- (UIImage *)calculatedImageForRect:(CGRect)imageRect
Problem
I want to make sure, that the imageView already contains the correct image as soon as the view becomes visible, so also during a simple push transition. Additionally, I don't want to generate the UIImage multiple times, since it is relatively expensive.
This becomes especially interesting with devices like the iPhone6 plus, that have a different screen resolution
Obviously at viewDidLoad
the frames are not set correctly. At that point the frames are set as in the preview of the storyboard - e.g. setup for representing IPhone5 resolution. Same is true for viewWillAppear
.
Here the frame would e.g. be (10,10,300,100) <-- let's call it wrong-frame
On viewDidAppear
the frame is correct (10,10,394,100) = the correct-frame <-- instead of 320 points width, the iPhone6Plus has 414 points. Nevertheless, here it is too late, since the view is already visible and setting the image would result in a flickering (empty image > new generated image).
I know the right time to work with the width is the viewDidLayoutSubviews
, which is first called with the wrong-frame at least once, then at one point with the correct-frame.
But as i said, i don't want to generate the image multiple times, so here's the dilemma:
How can I determine the right-frame before it is visible?
Of course I could calculate the right frame from the [UIScreen mainScreen]
resolution by working with the trailing & leading constraints of the UIImageView, but it feels dirty.