访问相机和PhotoLibrary

时间:2016-12-12 12:14:10

标签: ios swift camera photolibrary

在我的iOS应用程序中,我有一个ImageView和两个按钮,用于打开相机和photolibrary。当我点击其中一个按钮时,应用程序关闭。 (我在我的设备上运行应用程序,而不是模拟器) 我的代码需要更改什么?

class PhotoViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


@IBOutlet weak var ImageDisplay: UIImageView!
@IBOutlet weak var libraryOutlet: UIButton!
@IBOutlet weak var cameraOutlet: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

@IBAction func openCameraButton(_ sender: UIButton) {
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = .camera
        present(picker, animated: true, completion: nil)

    }

@IBAction func openLibraryButton(_ sender: UIButton) {
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = .photoLibrary
        present(picker, animated: true, completion: nil)
    }

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    ImageDisplay.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    dismiss(animated: true, completion: nil)
}

}

4 个答案:

答案 0 :(得分:2)

在iOS 10中,您需要通过向plist添加以下键来访问photoLibrary或相机,并且您需要使用正确的委托方法。

enter image description here

访问照片库:

MariaDB [sandbox]> SELECT * FROM T;
+----------+--------+
| from_row | to_row |
+----------+--------+
|        1 |      4 |
|        4 |      6 |
|        6 |      9 |
|        9 |     13 |
+----------+--------+
4 rows in set (0.00 sec)

MariaDB [sandbox]> SELECT * FROM USERS;
+-----+----------+--------------+--------+---------------------+
| id  | userName | photo        | status | ts                  |
+-----+----------+--------------+--------+---------------------+
|   1 | John     | john.png     |      1 | 2016-12-08 13:14:24 |
|   2 | Jane     | jane.png     |      1 | 2016-12-08 13:14:24 |
|   3 | Ali      |              |      1 | 2016-12-08 13:14:24 |
|   6 | Bruce    | bruce.png    |      1 | 2016-12-08 13:14:24 |
|   7 | Martha   |              |      1 | 2016-12-08 13:14:24 |
|   8 | Sidney   |              |      1 | 2016-12-08 13:14:24 |
|  10 | Charlie  | charlie.png  |      1 | 2016-12-08 13:14:24 |
|  12 | Elisa    |              |      1 | 2016-12-08 13:14:24 |
|  14 | Samantha | samantha.png |      1 | 2016-12-08 13:14:24 |
|  15 | Hannah   | hannah.png   |      1 | 2016-12-08 13:14:24 |
|  16 | Hannah   |              |      1 | 2016-12-08 13:14:24 |
|  17 | Kevin    | kevin1.png   |      1 | 2016-12-08 13:14:24 |
|  18 | Kevin    | kevin2.png   |      1 | 2016-12-08 13:14:24 |
|  19 | Ruth     |              |      1 | 2016-12-08 13:14:24 |
| 999 | xxx      | photo        |      1 | 2016-12-08 13:16:41 |
+-----+----------+--------------+--------+---------------------+
15 rows in set (0.00 sec)

MariaDB [sandbox]> SELECT U.ID, FROM_row as start_row
    -> FROM USERS U
    -> JOIN T ON U.ID
    -> BETWEEN T.FROM_row AND T.TO_row
    -> ;
+----+-----------+
| ID | start_row |
+----+-----------+
|  1 |         1 |
|  2 |         1 |
|  3 |         1 |
|  6 |         4 |
|  6 |         6 |
|  7 |         6 |
|  8 |         6 |
| 10 |         9 |
| 12 |         9 |
+----+-----------+
9 rows in set (0.00 sec)

访问设备相机:

@IBAction func library(_ sender: UIButton) {

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
        imagePicker.allowsEditing = true
        self.present(imagePicker, animated: true, completion: nil)
      } 
    }

选择并显示图片:

@IBAction func camera(_ sender: UIButton) {

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {    
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
        imagePicker.allowsEditing = false
        self.present(imagePicker, animated: true, completion: nil)
        }
    }

<强>输出:

enter image description here

答案 1 :(得分:0)

如果您在iOS10上运行,则必须在Info.plist中添加用于访问摄像头的条目

将此密钥放入Info.plist

隐私权 - 相机使用说明

http://useyourloaf.com/blog/privacy-settings-in-ios-10/

如果不是应用程序会像您的情况那样崩溃

答案 2 :(得分:0)

如果您正在ios 10中开发应用程序,则必须在info.plist中添加隐私权限设置,并描述您需要此隐私的地方。

隐私设置清单

蓝牙共享 - NSBluetoothPeripheralUsageDescription

日历 - NSCalendarsUsageDescription

CallKit - NSVoIPUsageDescription

相机 - NSCameraUsageDescription

联系人 - NSContactsUsageDescription

健康 - NSHealthShareUsageDescription&amp; NSHealthUpdateUsageDescription

HomeKit - NSHomeKitUsageDescription

位置 - NSLocationUsageDescription,NSLocationAlwaysUsageDescription,

NSLocationWhenInUseUsageDescription

媒体库 - NSAppleMusicUsageDescription

麦克风 - NSMicrophoneUsageDescription

Motion - NSMotionUsageDescription

照片 - NSPhotoLibraryUsageDescription

提醒 - NSRemindersUsageDescription

语音识别 - NSSpeechRecognitionUsageDescription

SiriKit - NSSiriUsageDescription

电视提供商 - NSVideoSubscriberAccountUsageDescription

答案 3 :(得分:0)

iOS 10不允许访问联系人,相机,照片库,用户位置等,直到我们提到为什么我们使用它。打开你的plist作为源代码在dict下添加以下代码现在再次运行它。 / p>

<!--  Photo Library -->
<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME) photo use</string>

<!--  Camera -->
<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) camera use</string>

<!--  Location -->
<key>NSLocationUsageDescription</key>
<string>$(PRODUCT_NAME) location use</string>

<!--  Contacts -->
<key>NSContactsUsageDescription</key>
<string>$(PRODUCT_NAME) contact use</string>