ios 如何在所有可用的 iPhone 分辨率下处理图像比例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25969533/
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
How to handle image scale on all the available iPhone resolutions?
提问by Leszek Szary
What sizes would be the best to use for images: background.png, [email protected] and [email protected] if we want to use this image for example to cover the full width and half height of the screen on all resolutions for iPhone portrait app?
什么尺寸最适合用于图像:background.png、[email protected] 和 [email protected] 如果我们想使用这个图像来覆盖所有分辨率下的屏幕的全宽和半高iPhone人像应用程序?
This is what we have now:
这就是我们现在所拥有的:
Device Points Pixels Scale Physical Pixels PPI Ratio Size
iPhone XS Max 896x414 2688x1242 3x 2688x1242 458 19.5:9 6.5"
iPhone XR 896x414 1792x828 2x 1792x828 326 19.5:9 6.1"
iPhone X 812x375 2436x1125 3x 2436x1125 458 19.5:9 5.8"
iPhone 6 Plus 736x414 2208x1242 3x 1920x1080 401 16:9 5.5"
iPhone 6 667x375 1334x750 2x 1334x750 326 16:9 4.7"
iPhone 5 568x320 1136x640 2x 1136x640 326 16:9 4.0"
iPhone 4 480x320 960x640 2x 960x640 326 3:2 3.5"
iPhone 3GS 480x320 480x320 1x 480x320 163 3:2 3.5"
Some people say that for edge to edge image (like a banner on the bottom from left to right edge of the screen) for iPhone 6 Plus they would prepare [email protected] with width 1242 and for iPhone 6 [email protected] with width 750 to match the iPhone 6 screen size however I do not think that this is a good idea because 1242 / 3 = 414 and 750 / 2 = 375 so naming them as @2x and @3x does not have sense. And then what width should have back.png - 375 or 414?
有人说,对于 iPhone 6 Plus 的边到边图像(如屏幕底部从左到右边缘的横幅),他们会准备宽度为 1242 的 [email protected] 和宽度为 1242 的 iPhone 6 [email protected]宽度 750 以匹配 iPhone 6 屏幕尺寸,但是我认为这不是一个好主意,因为 1242 / 3 = 414 和 750 / 2 = 375 所以将它们命名为 @2x 和 @3x 没有意义。然后什么宽度应该有 back.png - 375 或 414?
Graphics names are using @2x and @3x suffixes so if for example [email protected] has 30x30 resolution then logically thinking [email protected] should have 20x20 resolution and image.png should be 10x10. This means that if we want to have sharp full width image for each screen then we probably should create [email protected] with width 414*3=1242px, [email protected] with width 414*2=828px and back.png with width 414px. This however means that on every iPhone except for iPhone 6 Plus you will need to setup your uiimages to use for example aspect fit content mode and they will be scalled down so this again is not a perferct solution and probably would really slow down the application if we use a lot of scalling on older devices.
图形名称使用@2x 和@3x 后缀,因此如果例如 [email protected] 具有 30x30 分辨率,那么逻辑上认为 [email protected] 应该具有 20x20 分辨率,而 image.png 应该是 10x10。这意味着,如果我们想为每个屏幕都有清晰的全宽图像,那么我们可能应该创建宽度为 414*3=1242px 的 [email protected]、宽度为 414*2=828px 的 [email protected] 和宽度为 414*2=828px 的 back.png宽度 414 像素。然而,这意味着在除 iPhone 6 Plus 之外的每部 iPhone 上,您都需要设置您的 uiimages 以使用例如方面适合内容模式,并且它们将被调用,因此这又不是一个完美的解决方案,并且可能会真正减慢应用程序,如果我们在旧设备上使用了大量的调用。
So what do you think would be the best solution to solve this problem?
那么你认为解决这个问题的最佳方案是什么?
回答by Filip Radelic
You don't have to have each image in all scales if it won't be used. Make only the sizes you need and name them according to their width. For portrait full-device-width images, you need 320px wide at 1x and 2x, 375px wide at 2x and 414px wide at 3x.
如果不使用,您不必拥有所有比例的每个图像。仅制作您需要的尺寸并根据其宽度命名。对于纵向全设备宽度图像,1x 和 2x 时需要 320px 宽,2x 时需要 375px 宽,3x 时需要 414px 宽。
4" devices used "-568h" suffix for naming their launch images, so I'd recommend a similar naming scheme:
4" 设备使用“-568h”后缀来命名它们的启动图像,因此我建议使用类似的命名方案:
- ImageName-320w (@1x & @2x)
- ImageName-375w (@2x)
- ImageName-414w (@3x)
- ImageName-320w (@1x & @2x)
- ImageName-375w (@2x)
- ImageName-414w (@3x)
Then figure out what image you need at runtime:
然后弄清楚你在运行时需要什么图像:
NSNumber *screenWidth = @([UIScreen mainScreen].bounds.size.width);
NSString *imageName = [NSString stringWithFormat:@"name-%@w", screenWidth];
UIImage *image = [UIImage imageNamed:imageName];
This might break if other widths are added in future, but so far Apple has always required rebuilding the app to support new displays so I guess it's somewhat safe to assume they will continue doing that.
如果将来添加其他宽度,这可能会中断,但到目前为止,Apple 一直需要重建应用程序以支持新的显示器,所以我想假设他们会继续这样做是有点安全的。
回答by thewormsterror
I personally will be doing :
我个人会做:
ImageName@2x iPhone 4/4s
ImageName-568h@2x iPhone 5/5s
ImageName-667h@2x iPhone 6
ImageName-736h@3x iPhone 6Plus
ImageName@2x iPhone 4/4s
ImageName-568h@2x iPhone 5/5s
ImageName-667h@2x iPhone 6
ImageName-736h@3x iPhone 6Plus
The logic behind this is that it shows a difference between all devices, whereas width shares the same value on the iPhone 5s and iPhone 4s
这背后的逻辑是它显示了所有设备之间的差异,而宽度在 iPhone 5s 和 iPhone 4s 上共享相同的值
Edit:
编辑:
This is just the naming convention I am using for resources that are device dependant, such as a background image taking the whole screen, most of the time all you want is:
这只是我用于依赖于设备的资源的命名约定,例如占据整个屏幕的背景图像,大多数时候你想要的是:
ImageName@2x iPhone 4/4s/5/5s/6
ImageName@3x iPhone 6Plus/Zoom mode
ImageName@2x iPhone 4/4s/5/5s/6
ImageName@3x iPhone 6Plus/缩放模式
回答by Emiel
For the @2x and @3x discussion, you don't really have to care about that. Care about the point size of the screen, and make sure that there are @2x assets with twice the point size and @3x assets with thrice the point size in pixels. The device will automatically pick the right one. But reading your post I guess you already know this.
对于@2x 和@3x 的讨论,您实际上不必关心它。注意屏幕的点大小,并确保有两倍点大小的@2x 资产和三倍点大小的@3x 资产(以像素为单位)。设备将自动选择正确的。但是看了你的帖子,我猜你已经知道了。
For edge-to-edge images, then unfortunately you have to make it for all screen resolutions. So, for a portrait iPhone, it would be 320 points, 375 points and 414 points, where the 414 points one would have to be @3x. A better solution may be to make your images scalable by setting up the slicing in interface builder (if you use image catalogs, that is). But, depending on the image this may or may not be an option, depending whether the image has a repeatable or stretchable part. Scalable images set up like this have very little performance impact.
对于边缘到边缘的图像,不幸的是,您必须为所有屏幕分辨率制作它。因此,对于纵向 iPhone,它将是 320 分、375 分和 414 分,其中 414 分必须是 @3x。更好的解决方案可能是通过在界面构建器中设置切片来使您的图像具有可扩展性(如果您使用图像目录)。但是,根据图像,这可能是也可能不是一个选项,这取决于图像是否具有可重复或可拉伸的部分。像这样设置的可扩展图像对性能的影响很小。
回答by Denis Kozhukhov
the @2 and @3 is not the real image scaling, but only represent how much real pixel represent one virtual pixel on screen, some sort of hdpi/xhdpi/xxhdpi/blabla from android universe. it only show to system what image should be used for some device screen.
@2 和 @3 不是真实的图像缩放比例,而是仅代表屏幕上一个虚拟像素的真实像素数量,来自 android 世界的某种 hdpi/xhdpi/xxhdpi/blabla。它只向系统显示应该用于某些设备屏幕的图像。
so if you need to use whole screen image - prepare it dependently of real screen size.
因此,如果您需要使用整个屏幕图像 - 根据实际屏幕尺寸准备它。
回答by Leszek Szary
Depending on the graphics in some cases it might work fine when we use just a single image for example a banner with size 414 points width x 100 points height (largest possible width and some fixed height) and put it in a UIImageView that is pinned to the left and right edge of the screen, has fixed height 100 and set aspect fill mode for that UIImageView. Then on smaller devices left and right side of the image will be cut and we will see only the center part of the image.
在某些情况下取决于图形,当我们只使用单个图像时它可能会正常工作,例如大小为 414 点宽度 x 100 点高度(最大可能宽度和一些固定高度)的横幅并将其放入固定到的 UIImageView屏幕的左右边缘,具有固定高度 100 并为该 UIImageView 设置纵横填充模式。然后在较小的设备上,图像的左侧和右侧将被剪切,我们将只看到图像的中心部分。
回答by George
I've created category for this:
我为此创建了类别:
+ (UIImage *)sizedImageWithName:(NSString *)name {
UIImage *image;
if (IS_IPHONE_5) {
image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-568h",name]];
if (!image) {
image = [UIImage imageNamed:name];
}
}
else if (IS_IPHONE_6) {
image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-750w",name]];
}
else {
image = [UIImage imageNamed:name];
}
return image;
}
you can take full code here: https://gist.github.com/YGeorge/e0a7fbb479f572b64ba5
您可以在此处获取完整代码:https: //gist.github.com/YGeorge/e0a7fbb479f572b64ba5
回答by ???? ???? ???? ????
I think the best solution for edge to edge or full screen images, is to care about the real screen size in pixel (not in point), and you must check at runtime the model of the device and choose the appropriate image i.e:
我认为边缘到边缘或全屏图像的最佳解决方案是关心以像素为单位的真实屏幕尺寸(而不是点),并且您必须在运行时检查设备型号并选择适当的图像,即:
image-iphone4-4s.png (640x960/2) for 1/2 screen height
,
image-iphone5-5c-5s.png (640x1136/2) for 1/2 screen height
,
image-iphone6-6s.png (750x1334/2) for 1/2 screen height
,
image-iphone6plus-6splus.png (1242x2208/2) for 1/2 screen height
,
image-iphone4-4s.png (640x960/2) for 1/2 screen height
,
image-iphone5-5c-5s.png (640x1136/2) for 1/2 screen height
,
image-iphone6-6s.png (750x1334/2) for 1/2 screen height
,
image-iphone6plus-6splus.png (1242x2208/2) for 1/2 screen height
,
there is no need for @?x in this situation of the asker.
在提问者的这种情况下不需要@?x。
回答by user2024885
i think we should use different size of background images for different devices. Just use @3x scale images for background.
我认为我们应该为不同的设备使用不同大小的背景图像。只需使用@3x 比例图像作为背景。
You can detect device with below lines.
您可以使用以下几行检测设备。
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))
#define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)