最近啟動APP時要做一個引導頁,要適配不同的機型:
手機 | 屏幕尺寸 | 比例 |
---|---|---|
iPhone 1G | 320x480 | 0.667 |
iPhone 3G | 320x480 | 0.667 |
iPhone 3GS | 320x480 | 0.667 |
iPhone 4 | 640x960 | 0.667 |
iPhone 4S | 640x960 | 0.667 |
iPhone 5 | 640x1136 | 0.564 |
iPhone 5S | 640x1136 | 0.564 |
iPhone 5C | 640x1136 | 0.564 |
iPhone 6 | 750x1334 | 0.5623 |
iPhone 6 Plus | 1080x1920 (開發應按照1242x2208適配) | 0.5625 |
iPhone 6S | 750x1334 | 0.5623 |
iPhone 6S Plus | 1080x1920 (開發應按照1242x2208適配) | 0.5625 |
iPhone 7 | 750x1334 | 0.5623 |
iPhone 7 Plus | 1080x1920 (開發應按照1242x2208適配) | 0.5625 |
640x960 640x1136 750x1334 1080x1920 1242x2208
共有以上幾種尺寸
-(UIImage *)getFitFirstImage
{
float currentScreenScale = HSWidth/HSHeight;//當前屏幕比例
float a[3] = {640.0/960.0,750.0/1334.0,1242.0/2208.0};
int c = find(a, 3, currentScreenScale);
NSArray *imageScaleArray = @[@"640_960",
@"750_1334",
@"1242_2208"];
NSString *imageScale = imageScaleArray[c];
NSLog(@"%@",imageScale);
return [UIImage imageNamed:[NSString stringWithFormat:@"FirstLanuchImage%@.jpg",imageScale]];
}
int find(float *a,float n,float x)
{
int i;
float min=fabsf(*a-x);
int r=0;
for(i=0;i < n;++i)
{
if(fabsf(a[i]-x) < min)
{
min=fabsf(a[i]-x);
r=i;
}
}
return r;
}
主要通過當前屏幕的寬高比,找到數組中與當前屏幕最接近的圖片尺寸,從而使用不同尺寸的圖片,以達到適配效果。(這里只選擇了三個尺寸的圖)