Pregunta

I'm trying to resize a UIImage so that it totally covers it's parent view. It's not hard to do this when the image isn't transformed. How do I do it for any arbitrary rotation (e.g., 30 degrees, 97 degrees, 277 degrees, etc.)?

In the example below, I want the flower image to scale just enough so none of the white view is visible. I'd also like the origin as close to 0,0 as possible. In other words, I want want it aligned with the top, left corner.

Any ideas?

Bonus Question:

I'd also like the option of doing an aspect fit so the whole image is visible at the maximum possible size that fits in the white area.

enter image description here     enter image description here

¿Fue útil?

Solución

Here's a little push in the right direction.

scaled image

Given that W and H are the width and height of the parent view, and theta is the rotation angle of the image, then the image width (x) can be calculated as

x = W * cos(theta) + H * sin(theta);

From that result, you can calculate the scale factor for the image, which in turn gives you the image height. The rest is left as an exercise for the reader.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top