如何在 Xcode 的不同 iPhone 尺寸类中使用不同尺寸的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34792138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:25:50  来源:igfitidea点击:

How to use different size of image in different iPhone size classes in Xcode

iosiphonexcodeswift

提问by Ziare

I am currently developing an app and I wish it to be used in multiple size classes.

我目前正在开发一个应用程序,我希望它可以用于多种尺寸类别。

enter image description here

在此处输入图片说明

As you can see from the image attached, my trophy image does not resize itself at all.

正如您从所附图片中看到的,我的奖杯图片根本没有调整大小。

I have included 3 different sizes of the image in the assets folder and set the image into "Aspect fill" but it does not seems to work.

我在资产文件夹中包含了 3 种不同大小的图像,并将图像设置为“纵横比填充”,但它似乎不起作用。

Update: I have set the the constraints for my image to both top and bottom container but it results the same.

更新:我已将图像的约束设置为顶部和底部容器,但结果相同。

回答by Ramesh_T

Read this: https://stackoverflow.com/a/26085718/3051458

阅读:https: //stackoverflow.com/a/26085718/3051458

Add the images in xcAssets folder as per above explanation.

按照上述说明在 xcAssets 文件夹中添加图像。

You no need to give the extension of image (.png,.jpg etc.) It will pick the image as per the apple device.

您无需提供图像的扩展名(.png、.jpg 等)它会根据苹果设备选择图像。

Sample code:

示例代码:

UIImage *tropy = [UIImage imageNamed:@"tropy"];
UIImageView *tropyImageView = [[UIImageView alloc] initWithImage:tropy];

For more clear understanding read below totorial.

为了更清楚地理解,请阅读下面的教程。

http://guides.codepath.com/ios/Adding-Image-Assets#step-3-using-the-image-set

http://guides.codepath.com/ios/Adding-Image-Assets#step-3-using-the-image-set

回答by Ziare

If you are using autolayout, you can set aspect ratio constraints for width and height of the imageView to the cell content view, that way, if the cell grows image will grow too.

如果您使用自动布局,您可以将 imageView 的宽度和高度的纵横比约束设置为单元格内容视图,这样,如果单元格增长,图像也会增长。

回答by Fayza Nawaz

You can resize the imageViewand set height/width for different size classes. If you want to use differentimages for same image view across different size classes, i think it'll not be possible. checkout this tutorial: http://www.raywenderlich.com/113768/adaptive-layout-tutorial-in-ios-9-getting-started

您可以调整 imageView 的大小并为不同的尺寸类别设置高度/宽度。如果您想对不同尺寸类别的相同图像视图使用不同的图像,我认为这是不可能的。结帐本教程:http: //www.raywenderlich.com/113768/adaptive-layout-tutorial-in-ios-9-getting-started