Resize rotated image to fill a view

折月煮酒 提交于 2019-12-11 12:15:32

问题


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.

     


回答1:


Here's a little push in the right direction.

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.



来源:https://stackoverflow.com/questions/22773143/resize-rotated-image-to-fill-a-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!