您的位置:首页 > 产品设计 > UI/UE

UIImage的scale

2016-04-09 16:38 260 查看
iPhone OS4.0加入了scale factor,这表示point长度/pixel长度。

在分辨率为960*640的设备上,我们知道逻辑坐标系还是不变480*320,那么一个point有两个pixel长,所以scale=2.0。

在分辨率为480*320的设备商,point和pixel长度相同,scale=1.0。

为了程序自动适应分辨率,程序会自动给UIScreen.scale赋值,[UIScreen mainScreen].scale = 1.0 or 2.0。

我们编程,画图,窗口,字体等都是矢量图,通过scale能够让我们不管分辨率,按照逻辑坐标480*320来设计界面,效果在所有分辨率下都一致(物理尺寸位置一致)。但是和光栅相关的位图又如何适应呢?

比如一个32*32的图片,在高分辨率下,如果要位置大小都不变,那么32*32的图像点显示在64*64的像素(光栅)上。在不改程序的情况下,一个图像点的颜色对应4个颜色一致的像素点。这样图片不光模糊,而且还有锯齿。

所以,要需要一个64*64的新图片代替原图片。在不改程序代码的情况下如何实现?SDK要求我们再加一个图片,比如原图片是image.png,新加的就是image@2x.png,是64*64的。

在系统scale=2.0时,加载图像时先找@2x的,找不到在找原来的,所以就是只要给所有的图像资源文件都加上一个新的高清晰的@2x文件,程序就自动在高分辨屏幕上用高清晰图片,低分辨上用低清晰图像。

再来看看SDK是如何实现的。前提是程序代码没变,SDK内部实现变了。变了什么,主要就是UIImage新加了个scale。

按前面所讲的,读取image@2x.png时,scale=2.0,读取image.png时scale=1.0。

看同一张图片,有如下几个情况:

当UIScreen.scale=2.0时,读取image@2x.png,UIImage.scale=2.0 UIImage.size=16*16

当UIScreen.scale=1.0时,读取image.png, UIImage.scale=1.0 UIImage.size=32*32

当UIScreen.scale=2.0时,读取image.png, UIImage.scale=1.0 UIImage.size=32*32 (不存在@2x文件的时候)

看出来没有,图片的实际尺寸是size*scale。这里就可以给出UIImage的scale含义:

sacle = 实际大小 / 当前逻辑大小

当前逻辑大小 = 实际大小 / sacle

当前逻辑大小 = 实际大小 * (1/sacle)

sacle是什么,是缩小因子。1/sacle才是放大因子。

这跟一般情况的scale是放大因子是相反的啊!!!

比如UIScreen.scale=2.0,就表示我们的屏幕是被放大2.0倍的。

而UIImage.scale=2.0表示什么,表示当前的图片是被缩小了2.0倍的。

-----------------------------------------------end------------------------------------------------------------------

在同一张图片sacle=2.0和1.0时,UIImageView大小不变时,也就是说,虽然我没有高清晰图,但我把原来的图copy一个为@2x时,效果是不是一样的呢?

发现效果还真是不一样啊。@1.0是图片直接放大,有锯齿的。 @2.0是显卡放大,无锯齿了。



代码:

- (void)viewDidLoad {

[super viewDidLoad];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 50.0, 200.0, 32.0)] autorelease];

[self.view addSubview:label];

label.backgroundColor = [UIColor clearColor];

label.text = [NSString stringWithFormat:@"Screen.scale:%.1f", [UIScreen mainScreen].scale];

do {

UIImage *image = [UIImage imageNamed:@"test.png"];

UIImageView *imageView = [UIImageView alloc] initWithImage:image];

imageView.frame = CGRectMake(15.0, 100.0, 32.0, 32.0);

[self.view addSubview:imageView];

[imageView release];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 100.0, 300.0, 32.0)] autorelease];

[self.view addSubview:label];

label.backgroundColor = [UIColor clearColor];

label.text = [NSString stringWithFormat:@"image.scale:%.1f size:%.0f*%.0f", image.scale, image.size.width, image.size.height];

}while (0);

do {

UIImage *image = [UIImage imageNamed:@"test2.png"];

UIImageView *imageView = [UIImageView alloc] initWithImage:image];

imageView.frame = CGRectMake(15.0, 150.0, 32.0, 32.0);

[self.view addSubview:imageView];

[imageView release];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 150.0, 300.0, 32.0)] autorelease];

[self.view addSubview:label];

label.backgroundColor = [UIColor clearColor];

label.text = [NSString stringWithFormat:@"image.scale:%.1f size:%.0f*%.0f", image.scale, image.size.width, image.size.height];

}while (0);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: