2016-11-10 17 views
0

爲了開發應用程序,我設置了一個帶有.savedPhotosAlbum作爲源類型的UIImagePicker。UIImagePicker源類型不想更改

import UIKit 

class PictureViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

    /////////////////// OUTLETS //////////////////// 
    ///////////////// PROPERTIES /////////////////// 
    var imagePicker = UIImagePickerController() 

override func viewDidLoad() { 
     super.viewDidLoad() 

     imagePicker.delegate = self 

    } 

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
     // Where do we want our photos from and we don't want edited photos 
     imagePicker.allowsEditing = false 
     imagePicker.sourceType = UIImagePickerControllerSourceType.camera 
     imagePicker.cameraCaptureMode = .photo 
     imagePicker.modalPresentationStyle = .fullScreen 

     // Let's take the image from the picker 
     let image = info[UIImagePickerControllerOriginalImage] as! UIImage 
     snapImage.image = image 

     // When the user picks a picture, disabling the background color of the UIImageView 
     snapImage.backgroundColor = UIColor.clear 

     // Closing the image picker 
     imagePicker.dismiss(animated: true, completion: nil) 
    } 

    @IBAction func cameraTapped(_ sender: Any) { 
     present(imagePicker, animated: true, completion: nil) 
    } 

現在,我的應用程序完成後我想嘗試一下我的iPhone我的應用程序,所以我改變了這一點:

imagePicker.sourceType = .savedPhotosAlbum 

imagePicker.sourceType = UIImagePickerControllerSourceType.camera 

而且我在info.plist

添加
<key>NSCameraUsageDescription</key> 
    <string>We want to access your camera to ...</string> 

但是當我嘗試拍照時,它會一直向我展示我的相冊,這就是問題所在。 我希望能夠從手機的相機拍攝照片。

+0

你需要把更多的上下文,以便把你的代碼,你在哪裏檢查設備和呈現'picker' – Santosh

+0

@Santosh第一次發佈關於IOS對不起,我已經添加了我的一切 – user2462805

回答

1

你需要把這個代碼

imagePicker.allowsEditing = false 
imagePicker.sourceType = UIImagePickerControllerSourceType.camera 
imagePicker.cameraCaptureMode = .photo 
imagePicker.modalPresentationStyle = .fullScreen 

在任何viewDidLoadcameraTapped

問題 - 正如現在你把這個代碼的UIImagePicker委託方法,得到兩種採摘後調用圖像或拍攝圖像。

+0

謝謝你這麼多兩個解決方案和解釋! – user2462805