简体   繁体   English

如何获取UIimage大小取决于屏幕分辨率

[英]How to get UIimage size dependent on screen resolution

In my application while user try to upload image, I want image size based on the screen resolution. 在我的应用程序中,当用户尝试上传图像时,我想要基于屏幕分辨率的图像大小。

For example If image size is 9690 X 4400 then it should return its size depend on screen resolution instead of actual size. 例如,如果图像大小为9690 X 4400,则它应根据屏幕分辨率而不是实际大小返回其大小。

For mac application (OSX) we can do with this statement and its return me size 561 X 264 对于Mac应用程序(OSX),我们可以使用此语句,它的返回大小为561 X 264

NSImage *imageUpload = [[NSImage alloc] initWithContentsOfFile:fileName];

Same like mac how can we do for iOS app, any idea? 就像mac一样,我们该如何为iOS应用做些什么? plz help. 请帮助。

Is there any way to get this? 有什么办法可以做到这一点? I did google but not finding any results. 我做了谷歌,但没有找到任何结果。

Thanks 谢谢

Try something like this: 尝试这样的事情:

First you should to calculate aspect ratio of your view: 首先,您应该计算视图的长宽比:

double viewAspectRatio = myView.bounds.size.width / myView.bounds.size.height;

Second you should to calc aspect ratio of your image: 其次,您应该计算图像的长宽比:

double imageAspectRatio = myImage.size.width / myImage.size.height;

So finally 所以最后

CGSize scaledSize;
if (imageAspectRatio > viewAspectRatio) {
    scaledSize = CGSizeMake(myView.bounds.size.width, myView.bounds.size.width / imageAspectRatio);
} else {
    scaledSize = CGSizeMake(myView.bounds.size.height * imageAspectRatio, myView.bounds.size.height);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM