如何使用 UIImagePickerController 从实际设备访问照片

How to access photos from actual device using UIImagePickerController

目前我正在使用这个代码:

@IBAction func selectPicture(sender: UIButton) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
        var imag = UIImagePickerController()
        imag.delegate = self
        imag.sourceType = .Camera;
        imag.mediaTypes = [kUTTypeImage as String]
        imag.allowsEditing = false
        imag.modalPresentationStyle = .Popover
        self.presentViewController(imag, animated: true, completion: nil)
    }
}

但是当我 运行 它在 iPhone 设备的 iPad 上时,它只询问我相机许可,我无法从任何照片中选择照片album/camera滚。

我该怎么做才能从设备访问照片?

你设置错了sourceType:

imag.sourceType = .Camera;

将其更改为:

imag.sourceType = .SavedPhotosAlbum;

imag.sourceType = .PhotoLibrary

常量在UIImagePickerControllerSourceType Enumeration中定义,定义如下:

UIImagePickerControllerSourceType

The source to use when picking an image or when determining available media types. Declaration

Swift

enum UIImagePickerControllerSourceType : Int
{
    case PhotoLibrary
    case Camera
    case SavedPhotosAlbum
}

Constants

PhotoLibrary

Specifies the device’s photo library as the source for the image picker controller.

Camera

Specifies the device’s built-in camera as the source for the image picker controller. Indicate the specific camera you want (front or rear, as available) by using the cameraDevice property.

SavedPhotosAlbum

Specifies the device’s Camera Roll album as the source for the image picker controller. If the device does not have a camera, specifies the Saved Photos album as the source.

Discussion

A given source may not be available on a given device because the source is not physically present or because it cannot currently be accessed.