一部好看的電影《看不見的客人》,老年人被逼無奈的情況下,激發(fā)了他們的最大潛能,逼兇手認罪伏法。有些時候,人都是被逼的...
28號面試了一位同僚,想起了之前騰訊電話面試我的一個問題,@2x和@3x的圖片有啥區(qū)別,如果將@2x的圖片放在@3x的屏幕上會有什么效果,反之呢。候選人的回答讓我想起了曾經(jīng)的自己。
2007年初代iPhone 3GS,320x480像素。一個點是一個像素。
2010年iPhone4發(fā)布,使用Retina顯示屏,尺寸還是320*480,但像素為640x960。一個點是兩個像素。
2014年iPhone6s Plus發(fā)布,尺寸是414736,像素為12422208。一個點是三個像素。
假設(shè)圖片 example.png,大小為 30 x 40像素(這里的單位是像素,數(shù)字圖片的單位通常都為像素)。當(dāng)這張example.png在iPhone 4中使用時候,都占據(jù)屏幕上30 x 40個點。而因為iPhone 4中1個點等于2個像素,也就是30 x 40像素的圖片,占據(jù)了60 x 80像素的屏幕,因此這圖片在iPhone 4中看起來就會模糊。所以圖片的像素應(yīng)該為60*80像素。
在iPhone 6 Plus中,還出現(xiàn)3x模式,原理是一樣的。
開發(fā)中美工切圖要@2x和@3x的各一張,( @1x的(iPhone 3GS)已經(jīng)淘汰了,所以不用切圖 )。

1547050556930.jpg
例如:
example@2x.png // 60 x 80像素
example@3x.png // 90 x 120像素
當(dāng)程序中使用example.png的時候,會根據(jù)屏幕模式自動選擇對應(yīng)的圖片。屏幕2x模式,就會選擇
example2x.png, 3x模式就會優(yōu)先選擇example@3x.png,假如example@3x.png不存在,就選擇
example2x.png。
若@3x的圖片放在@2x的屏幕上面,不會有任何問題,因為6080像素的圖片堆積在3040的像素里,圖片會更清晰。

Simulator Screen Shot - iPhone 6s - 2019-01-06 at 21.09.11.png
若@2x的圖片放在@3x的屏幕上面,3040像素的圖片放在需要放6080像素的圖片里,圖片會模糊失真。
@2x圖片放在@3x屏幕上
IMG_2393.PNG
@3x圖片放在@3x屏幕上
IMG_2394.PNG
加載圖片方式

20160929093507359.png
-(void)setImageView1{
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 80, 240, 150)];
imageView.backgroundColor = [UIColor whiteColor];
//只有test@2x與test@3x圖片
//4s 5 5s 6 6s 會自動加載test@2x圖片
//6Plus 6sPlus 會自動加載test@3x圖片
imageView.image = [UIImage imageNamed:@"front"];
[self.view addSubview:imageView];
}
-(void)setImageView2{
//此處的路徑是物理路徑如果是邏輯路徑是獲取不到資源的
//這里填寫test@2x或者test@3x都可以(只要這個文件在wwwwww這個文件夾真實存在即可),主要是獲得這個物理路徑。
//獲得到這個路徑之后 后邊才會根據(jù)設(shè)備自動加載@2x圖片或者@3x圖片。
NSString *path = [[NSBundle mainBundle] pathForResource:@"wwwwww/test@2x" ofType:@"png"];
NSLog(@"path = %@",path);
//因為www是邏輯路徑,用此方法是加載不到這個文件的
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"www/test@2x" ofType:@"png"];
//所以path1的值為null;
NSLog(@"path1 = %@",path1);//path1 = null;
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 80, 240, 150)];
imageView.backgroundColor = [UIColor blueColor];
//4s 5 5s 6 6s 會自動加載test@2x圖片
//6Plus 6sPlus 會自動加載test@3x圖片
imageView.image = [UIImage imageWithContentsOfFile:path];
[self.view addSubview:imageView];
}
imageNamed與imageWithContentOfFile的區(qū)別
- myImage = [UIImage imageNamed:@"icon.png"];這種方法在一些圖片很少,或者圖片很小的程序里是ok的。但是,在大量加載圖片的程序里,請千萬不要這樣做。為什么呢 ??????
這種方法在application bundle的頂層文件夾尋找由供應(yīng)的名字的圖象 。 如果找到圖片,裝載iPhone系統(tǒng)緩存圖象。那意味圖片是(理論上)放在內(nèi)存里作為cache的。試想你圖片多了,是什么后果,圖片cache極有可能不會響應(yīng) memory warnings and release its objects。
- NSString *path = [[NSBundle mainBundle] pathForResource:@”icon” ofType:@”png”];
myImage = [UIImage imageWithContentsOfFile:path];
- NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:extension];
NSData *image = [NSData dataWithContentsOfFile:filePath];
[UIImage imageWithData:image];