【iOS開發-3】sandbox沙箱介紹以及如何取得沙箱裡面各檔案的路徑,圖片匯入方式和圖片路徑擷取

來源:互聯網
上載者:User

標籤:style   blog   http   io   os   ar   for   檔案   資料   

其實每個APP的程式都是單獨的一個檔案夾,每個APP之間相互之間獨立,所以它們的檔案夾也是獨立的,這個檔案夾就是沙箱。沙箱好處,安全。壞處,程式之間彼此共用資料較為困難。

(1)如何找到應用程式的沙箱源檔案?

一般沙箱源檔案都存放在~/Library/Application Support/iPhone Simulator/版本號碼/Applications/的檔案夾中,但是這個Library(中文名叫資產庫)檔案夾是隱藏的。所以需要方法才能點擊進去:在Finder中按CMD+SHIFT+G,然後再前往的框中輸入~/Library,也就是直接前往,而不是通過點擊前往。

當然也是可以用終端一直cd進去,注意有的目錄名有兩個單詞中間空格前面要加 \ 轉義,如cd ./Library/Application\ Support/iPhone\ Simulator/

後面選擇類比的版本,然後進入Application,裡面都是應用程式,當然是用雜湊碼命名,裡面有4個檔案:

Documents、Library、tmp和hello.app,存放永久和臨時資料的,這就是每個沙箱裡面的主要檔案。


(2)如何用代碼獲得沙箱裡面的檔案路徑?

註明:NSBundle就是擷取.app檔案裡面資源的一種方式。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    //擷取整個程式所在目錄    NSString *homePath=NSHomeDirectory();    NSLog(@"%@",homePath);    //擷取.app檔案目錄    NSString *appPath=[[NSBundle mainBundle]bundlePath];    NSLog(@"%@",appPath);    //Documents目錄    NSArray *arr1=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSLog(@"%@",[arr1 objectAtIndex:0]);    //Library目錄    NSArray *arr2=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);    NSLog(@"%@",[arr2 objectAtIndex:0]);    //Caches目錄,在Library下面    NSArray *arr3=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);    NSLog(@"%@",[arr3 objectAtIndex:0]);    //tmp目錄    NSString *tmpPath=NSTemporaryDirectory();    NSLog(@"%@",tmpPath);    //用整個程式目錄加上tmp就拼湊出tmp目錄,其他目錄都可這樣完成    NSString *tmpPath_1=[homePath stringByAppendingPathComponent:@"tmp"];    NSLog(@"%@",tmpPath_1);    // Override point for customization after application launch.    return YES;}
結果:

2014-09-23 21:05:58.966 hello[7763:546804] /Users/Andy/Library/Developer/CoreSimulator/Devices/64EDA842-5B0C-448D-BF2B-B063D09B60CB/data/Containers/Data/Application/08972B0F-5ECF-42B2-934B-892E8E3F7F6D2014-09-23 21:05:58.966 hello[7763:546804] /Users/Andy/Library/Developer/CoreSimulator/Devices/64EDA842-5B0C-448D-BF2B-B063D09B60CB/data/Containers/Bundle/Application/E7D6885C-9CC1-461D-867C-8D0DA5AF183A/hello.app2014-09-23 21:05:58.967 hello[7763:546804] /Users/Andy/Library/Developer/CoreSimulator/Devices/64EDA842-5B0C-448D-BF2B-B063D09B60CB/data/Containers/Data/Application/08972B0F-5ECF-42B2-934B-892E8E3F7F6D/Documents2014-09-23 21:05:58.967 hello[7763:546804] /Users/Andy/Library/Developer/CoreSimulator/Devices/64EDA842-5B0C-448D-BF2B-B063D09B60CB/data/Containers/Data/Application/08972B0F-5ECF-42B2-934B-892E8E3F7F6D/Library2014-09-23 21:05:58.967 hello[7763:546804] /Users/Andy/Library/Developer/CoreSimulator/Devices/64EDA842-5B0C-448D-BF2B-B063D09B60CB/data/Containers/Data/Application/08972B0F-5ECF-42B2-934B-892E8E3F7F6D/Library/Caches2014-09-23 21:05:58.967 hello[7763:546804] /Users/Andy/Library/Developer/CoreSimulator/Devices/64EDA842-5B0C-448D-BF2B-B063D09B60CB/data/Containers/Data/Application/08972B0F-5ECF-42B2-934B-892E8E3F7F6D/tmp/2014-09-23 21:05:58.967 hello[7763:546804] /Users/Andy/Library/Developer/CoreSimulator/Devices/64EDA842-5B0C-448D-BF2B-B063D09B60CB/data/Containers/Data/Application/08972B0F-5ECF-42B2-934B-892E8E3F7F6D/tmp

(3)匯入圖片的方式以及如何擷取路徑

我們往程式中匯入一個img檔案夾或者一張圖片時有兩種方式:Creat groups和Creat folder reference,前者表示匯入進來的圖片都在.app(它其實是哥檔案夾)中,列示在左側的img檔案夾只是用來分組而不是真正的目錄;而後者表示不僅把圖片匯入進來,還把img目錄匯入進來,即.app下有個img檔案夾,裡面存放的時圖片。



我們看到如下擷取圖片路徑都用到NSBundle,其實因為都是在.app檔案夾中,而NSBundle就是擷取.app裡資源的,所以在強化記憶一遍。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    //Create groups方式,圖片直接在.app裡,以下兩種方式相同    NSString *path1=[[NSBundle mainBundle]pathForResource:@"1" ofType:@"png"];    NSString *path2=[[NSBundle mainBundle]pathForResource:@"1.png" ofType:nil];    NSLog(@"%@,%@",path1,path2);        //Create folder reference方式,圖片在img裡,以下三種方式相同    NSString *path3=[[NSBundle mainBundle]pathForResource:@"1" ofType:@"png" inDirectory:@"img"];    NSString *path4=[[NSBundle mainBundle]pathForResource:@"1.png" ofType:nil inDirectory:@"img"];    NSString *path5=[[NSBundle mainBundle]pathForResource:@"img/1.png" ofType:nil inDirectory:nil];    NSLog(@"%@,%@,%@",path3,path4,path5);    // Override point for customization after application launch.    return YES;}


【iOS開發-3】sandbox沙箱介紹以及如何取得沙箱裡面各檔案的路徑,圖片匯入方式和圖片路徑擷取

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.