Asset Catalogs (資產(chǎn)分類),它可以管理物理圖像文件和這些文件的上下文信息。
參考:
- GitHub 源碼:shinobicontrols/iOS7-day-by-day
- 天天品嘗iOS7甜點(diǎn) :: Day 1 :: NSURLSession
- 知乎專欄:iOS 溫習(xí)之路 ” Bundle “
Asset Catalog - 資產(chǎn)分類
- 資產(chǎn)分類目錄由一些圖像的集合、app圖標(biāo)和app啟動(dòng)頁(yè)構(gòu)成。
- 新建工程時(shí),Xcode 會(huì)自動(dòng)創(chuàng)建一個(gè)名為
Assets.xcassets的文件夾。 - Asset catalogs 是存在在硬盤上面的一個(gè)目錄,由Xcode進(jìn)行管理。它以特定的方式進(jìn)行文件的排列,包含一個(gè)json文件存儲(chǔ)目錄相關(guān)聯(lián)的元數(shù)據(jù):

App icons and launch images
Asset catalog(資產(chǎn)分類)默認(rèn)自動(dòng)創(chuàng)建,名為Assets.xcassets,包含AppIcon的入口。提供了構(gòu)建應(yīng)用的所有需要的圖標(biāo)的尺寸:
LaunchImage 目錄需要自己創(chuàng)建:鼠標(biāo)右擊 —— App Icons & Launch Images —— New iOS Launch Image

Custom imagesets - 自定義圖像集
和標(biāo)準(zhǔn)的集合一樣,你可以使用 asset catalogs 來(lái)管理你自己的圖片,圖片都是包含在ImageSet中的,并且可以使用管理對(duì)應(yīng)的retina和非retina圖像。

使用方法:UIImage:imageNamed:
UIImage *image = [UIImage imageNamed:@"Australia"];
?? 加載圖片還涉及到緩存問題,參考之前寫的文章:
Slicing images - 切片圖像
Asset catalogs 的另外一個(gè)主要的特性就是能夠讓圖像進(jìn)行切片工作,自從iOS2開始程序就能夠使用切片的圖像了,但是Xcode中的這個(gè)新特性會(huì)變得非常的簡(jiǎn)單。
使用切片變換圖像,是創(chuàng)建虛擬的元素基本的技術(shù)。例如按鈕,需要拉伸這個(gè)按鈕圖片的大小達(dá)到一個(gè)新的大小,并且要設(shè)置邊緣不被拉伸,只拉伸中間的部分。
在 Asset catalog 中就可以使用Xcode提供的功能進(jìn)行切片操作了,開啟切片功能,只需要點(diǎn)擊Show Slicing按鈕,你可以選擇水平、豎直或者兩者都拉伸的方式,如果你選擇之后,圖像將會(huì)重新加載并且上面會(huì)出現(xiàn)拉伸輔助線,你可以通過(guò)拖拽輔助線,來(lái)實(shí)現(xiàn)自己需要拉伸的效果。

使用這些切片圖像編程非常的簡(jiǎn)單,和之前一樣創(chuàng)建一個(gè)UIImage對(duì)象,然后當(dāng)你調(diào)整UIImageView的大小,內(nèi)部的image就會(huì)自動(dòng)根據(jù)切片的圖像來(lái)變換大小。
UIImage *btnImage = [UIImage imageNamed:@"ButtonSlice"];
// Let's make 2
UIImageView *iv = [[UIImageView alloc] initWithImage:btnImage];
iv.bounds = CGRectMake(0, 0, 150, CGRectGetHeight(iv.bounds));
iv.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 2, 300);
[self.view addSubview:iv];
// And a stretched version
iv = [[UIImageView alloc] initWithImage:btnImage];
iv.bounds = CGRectMake(0, 0, 300, CGRectGetHeight(iv.bounds));
iv.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 2, 350);
[self.view addSubview:iv];

步驟詳解:
- 點(diǎn)擊
Show Slicing按鈕?

-
選擇拉伸模式:
支持三種模式:水平拉伸、水平和垂直同時(shí)拉伸、垂直拉伸。
-
設(shè)置拉伸句柄:
-
代碼添加圖片:
UIImage *image = [UIImage imageNamed:@"bird"]; // default UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; imageView.bounds = CGRectMake(0, 0, 50, 37.5); imageView.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 2, 300); [self.view addSubview:imageView]; // stretch UIImageView *stretchImgView = [[UIImageView alloc] initWithImage:image]; stretchImgView.bounds = CGRectMake(0, 0, 100, 75); stretchImgView.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 2, 400); [self.view addSubview:stretchImgView]; -
效果:用【填充部分】去覆蓋【可擴(kuò)展部分】
ImageOptim - 超好用的圖像壓縮工具
ImageOptim 是一個(gè)免費(fèi)的圖像壓縮工具。iOS 工程默認(rèn)使用 pngcrush 命令來(lái)壓縮數(shù)據(jù),不過(guò)其壓縮比率不是很高。而使用 ImageOptim 可以達(dá)到最大的圖片壓縮效果。
實(shí)現(xiàn)原理:使用各種開源的圖像壓縮工具,然后取壓縮效果最好的那一個(gè)。
使用方法也非常簡(jiǎn)單:在 Finder 中打開圖片所在的文件夾,將圖片拖拽進(jìn) ImageOptim 界面中即可。
其他圖像壓縮工具:
- Optimizilla ——在線圖像優(yōu)化壓縮器
附錄:
摘自官方人機(jī)交互指南:iOS Human Interface Guidelines
圖片大小和分辨率(Image Size and Resolution)
| 設(shè)備 | 比例系數(shù) |
|---|---|
| iPhone 7 Plus and iPhone 6s Plus | @3x |
| 其余高分辨率的 iOS 設(shè)備 | @2x |
App Icon
| 設(shè)備或狀況 | 圖標(biāo)大小 |
|---|---|
| iPhone | 180px × 180px (60pt × 60pt @3x) |
| 120px × 120px (60pt × 60pt @2x) | |
| iPad Pro | 167px × 167px (83.5pt × 83.5pt @2x) |
| iPad, iPad mini | 152px × 152px (76pt × 76pt @2x) |
| App Store | 1024px × 1024px |
Spotlight, Settings, and Notification Icons
Spotlight
| 設(shè)備 | Spotlight 圖標(biāo)大小 |
|---|---|
| iPhone | 120px × 120px (40pt × 40pt @3x) |
| 80px × 80px (40pt × 40pt @2x) | |
| iPad Pro, iPad, iPad mini | 80px × 80px (40pt × 40pt @2x) |
Settings
| 設(shè)備 | Settings 圖標(biāo)大小 |
|---|---|
| iPhone | 87px × 87px (29pt × 29pt @3x) |
| 58px × 58px (29pt × 29pt @2x) | |
| iPad Pro, iPad, iPad mini | 58px × 58px (29pt × 29pt @2x) |
Notification
| 設(shè)備 | Notification 圖標(biāo)大小 |
|---|---|
| iPhone | 60px × 60px (20pt × 20pt @3x) |
| 40px × 40px (20pt × 20pt @2x) | |
| iPad Pro, iPad, iPad mini | 40px × 40px (20pt × 20pt @2x) |
自定義圖標(biāo)大小
| 導(dǎo)航欄和工具欄圖標(biāo)尺寸 | 標(biāo)簽欄圖標(biāo)尺寸 | |
|---|---|---|
| 推薦 | 75px × 75px (25pt × 25pt @3x) | 75px × 75px (25pt × 25pt @3x) |
| 50px × 50px (25pt × 25pt @2x) | 50px × 50px (25pt × 25pt @2x) | |
| 最大 | 83px × 83px (27.67pt × 27.67pt @3x) | 144px × 96px (48pt × 32pt @3x) |
| 56px × 56px (28pt × 28pt @2x) | 96px × 64px (48pt × 32pt @2x) |
啟動(dòng)畫面(Launch Screen)
靜態(tài)啟動(dòng)畫面圖片
| 設(shè)備 | Portrait size | Landscape size |
|---|---|---|
| iPhone 7 Plus, iPhone 6s Plus | 1242px × 2208px | 2208px × 1242px |
| iPhone 7, iPhone 6s | 750px × 1334px | 1334px × 750px |
| iPhone SE | 640px × 1136px | 1136px × 640px |
| 12.9-inch iPad Pro | 2048px × 2732px | 2732px × 2048px |
| 9.7-inch iPad Pro, iPad Air 2, iPad mini 4, iPad mini 2 | 1536px × 2048px | 2048px × 1536px |


