2017-04-09 75 views
0

我需要解決一個任務。 我的視圖上有三個按鈕。每個人都會打電話給相機。所有的按鈕是相互獨立other.And我需要保存文件的命令:用三個集名稱保存圖像,從不同的按鈕

button_1->使光電>保存到文件目錄並將其命名爲「land.png」

button_2->使光電>保存到文件目錄並將其命名爲「water.png」

button_3->使光電>保存到

我做了三個按鈕,可以和我的文檔目錄,並命名爲「fire.png」調用pickerImage控制器,我可以保存到目錄。但該文件正在被重寫。我只能製作一張照片,應用會將其他照片保存到同一個文件中。甚至沒有重命名它。但最後我需要三個不同的.png文件。 請幫助和建議這種情況!

也許我錯了在試圖給文件名稱的地方?在我將其寫入文檔文件夾之前,我嘗試給出一個名稱。

回答

0

如果你想保存圖像照片

你可以通過設置圖像IPTC元數據字段「對象名稱」。

檢查以下鏈接

http://ootips.org/yonat/how-to-set-the-image-name-when-saving-to-the-camera-roll/

如果你想將圖像保存到文件目錄

let documentsDirectoryURL = try! NSFileManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true) 
// create a name for your image based on button clicked 
let fileURL = documentsDirectoryURL.URLByAppendingPathComponent("land.jpg") 

if !NSFileManager.defaultManager().fileExistsAtPath(fileURL.path!) { 
    if UIImageJPEGRepresentation(image, 1.0)!.writeToFile(fileURL.path!, atomically: true) { 
     print("file saved to directory") 
    } else { 
     print("error in saving file") 
    } 
} else { 
    print("file already exists with this name") 
} 
+0

所以,你說有沒有辦法,我可以給文件命名口罩?您提供的鏈接是在保存到相機膠捲時嘗試給出名稱。我不需要那個。因爲我需要保存到文檔文件夾。還有其他建議嗎? –

+0

我已編輯答案,請檢查。 –