標籤:device this -o net 素數 his ring nslog inter
A UIScreen object contains the bounding rectangle of the device’s entire screen. When setting up your application’s user interface, you should use the properties of this object to get the recommended frame rectangles for your application’s window.
UIScreen對象包含了整個螢幕的邊界矩形。當構造應用的使用者介面介面時,你應該使用該對象的屬性來獲得推薦的矩形大小,用以構造你的程式視窗。
以下列出的屬性和操作是我用過的。
+ mainScreen Returns the screen object representing the device’s screen.
bounds property Contains the bounding rectangle of the screen, measured in points. (read-only)
applicationFrame property The frame rectangle for the app window. (read-only)
scale property The natural scale factor associated with the screen. (read-only)
[plain]
CGRect bound = [[UIScreen mainScreen] bounds]; // 返回的是帶有狀態列的Rect
CGRect frame = [[UIScreen mainScreen] applicationFrame]; // 返回的是不帶有狀態列的Rect
float scale = [[UIScreenmainScreen] scale]; // 得到裝置的自然解析度
對於scale屬性需要做進一步的說明:
以前的iphone 裝置螢幕解析度都是320*480,後來apple 在iPhone 4中採用了名為Retina的顯示技術,iPhone 4採用了960x640像素解析度的顯示螢幕。由於螢幕大小沒有變,還是3.5英寸,解析度的提升將iPhone 4的顯示解析度提升至iPhone 3GS的四倍,每英寸的面積裡有326個像素。
scale屬性的值有兩個:
scale = 1; 的時候是代表當前裝置是320*480的解析度(就是iphone4之前的裝置)
scale = 2; 的時候是代表解析度為640*960的解析度
判斷螢幕類型:
[plain]
// 判斷螢幕類型,普通還是視網膜
float scale = [[UIScreen mainScreen] scale];
if (scale == 1) {
bIsRetina = NO;
NSLog(@"普通螢幕");
}else if (scale == 2) {
bIsRetina = YES;
NSLog(@"視網膜螢幕");
}else{
NSLog(@"unknow screen mode !");
}
對dpi和ppi的理解:
DPI是每英寸的點數,可以簡單理解為點的密度。
PPI是每英寸的像素數,可以簡單理解為像素密度。
點和像素有區別嗎?很多時候,一個點 = 一個像素。但是,並不盡然,如iPhone的視網膜螢幕,它一個點包含了四個像素,大大提高了顯示清晰度。
使用UIScreen擷取的bounds和frame,都是點的尺寸,而非像素尺寸。例如,在視網膜屏的iPhone4上,我擷取的applicationFrame大小是320x460,很明顯它代表的是點的數量。假如你要顯示一張圖,如果該圖原大小是100x200,那麼顯示在這樣的螢幕上,它的實際顯示出來的尺寸將只有原來大小的一半,但它的像素數並未改變。更加需要考慮的是,如果你要對這張圖進行縮放,那麼縮放率該按照實際顯示尺寸計算,還是按照實際像素數計算?這一塊很重要,曾走了不少彎路,答案是前者。
[[UIScreen mainScrenn] scale]
什麼是解析度?不然我們先來看一張圖。
[[UIScreen mainScreen] scale]是計算螢幕解析度的。
[[UIScreen main] scale] == 1; //代表320 x 480 的解析度
[[UIScreen main] scale] == 2; //代表640 x 960 的解析度
[[UIScreen main] scale] == 3; //代表1242 x 2208 的解析度
iOS - UIScreen的 bound、frame、scale屬性