2017-02-14 20 views
0

每當我使用的應用程序,它要求合影,這樣的事情會彈出: enter image description here斯威夫特 - 以照片或選擇現有

做任何你們中有什麼是真正回事參考這裏?是否有一些內置函數或開源庫用於此?我似乎無法在互聯網上找到任何具體資源。

謝謝!

+0

看起來像你的基本模態警報給我。沒什麼更多。 *真實*問題是您在「拍照」或「選擇現有」時看到的內容 - 他們是否使用UIImagePickerController? – dfd

回答

2

在您的截圖中,你看到的UIAlertController有兩個動作:

let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) 
let cameraAction = UIAlertAction(title: "Take Photo", style: .default, handler: nil) 
let photoLibraryAction = UIAlertAction(title: "Choose Existing", style: .default handler: nil) 

actionSheet.addAction(camerAction) 
actionSheet.addAction(photoLibraryAction) 

要訪問圖片庫,或用相機拍攝新照片,你可能想看看UIImagePickerController

+1

正是我所需要的 - 這是一個UIAlertController。謝謝! – Ryan

0

看起來像圖書館呈現UIAlertControllerUIAlertControllerStyle枚舉actionSheet。取決於選擇,然後使用sourceTypephotoLibrarycamera實例化UIImagePickerController

這是獲取用戶圖像的標準行爲。

1

你必須通過檢查相機源可用,然後添加相關動作控制器動態地構建一個UIActionController

let actionController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) 

if UIImagePickerController.isSourceTypeAvailable(.camera) { 
    let action = UIAlertAction(title: "Take Photo", style: .default, handler: { _ in 
     print("User tapped 'Take Photo'") 
    }) 
    actionController.addAction(action) 
} 

您還需要創建「選擇現有」另一個動作和「取消」按鈕。

然後只顯示當前視圖控制器中的動作控制器。

+0

謝謝!這非常有幫助。 – Ryan