2012-10-01 38 views
0

我正在使用一個xcode項目(遊戲與cocos2d),它使用了大量的精靈。問題是我想將URL中的圖像下載到設備上的本地目錄中,以便稍後使用它們來創建精靈。在哪裏下載圖像到xcode

那麼下載圖像的最佳位置是什麼?我可以在我的項目文件夾中創建文件夾引用,但這是一個好方法嗎?這是否有一個標準的做法?

我看到有iPhone可以用於此? 請讓我知道,如果有資源,可以幫助我瞭解這些好....

感謝名單

+1

嘗試在'NSDocumentsDirectory'或圖書館目錄保存得到它。 – Bazinga

回答

1

您可以將URl中的所有圖像下載到DocumentDirectory中,以後可以將其用作遊戲的一部分。我使用下面的代碼來下載圖像

NSArray* path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); 
NSString* docPath = [path objectAtIndex:0]; 
     docPath = [docPath stringByAppendingPathComponent:@"ImageName.png"]; 
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[array objectAtIndex:o] valueForKey:@"Image_Question"]]]]; 
NSData* imgData = UIImagePNGRepresentation(image); 
[imgData writeToFile:docPath atomically:YES]; 

而當你想要檢索的應用程序,你可以使用下面的代碼的任何使用

UIImage* imageFormDoc = [UIImage imageWithContentsOfFile:docPath]; 

,當你要刪除的圖像形成文檔目錄你可以使用下面的代碼。

[[NSFileManager defaultManager] removeItemAtPath:docPath error:& error];

docPath是Document Image with Image的完整路徑,那麼您可以使用NSLog打印以獲取詳細信息。 強調文本

+0

非常感謝:) – user1594795

0

是, 可以將所有圖像保存到應用程序的文件夾,後來,你可以使用應用程序中的所有圖像。

+0

所以這個「文檔文件夾」是提供用於處理動態內容的庫的一部分? – user1594795

+0

是的,你可以在動態內容中使用它。 – Sham

0

儘量節省NSDocumentsDirectory或圖書館目錄和u可以從plist中

+0

你能告訴我什麼是圖書館目錄的路徑? – user1594795