美文网首页iOS Developer
[iOS]iOS10中通过摄像头或者从相册中获取图片的方法

[iOS]iOS10中通过摄像头或者从相册中获取图片的方法

作者: 小池laucherish | 来源:发表于2017-01-19 16:53 被阅读863次

现在的App里一般都会有设置头像这个功能,网上介绍的大多是在旧版iOS上面使用Objective-C实现的。那么在最新的iOS 10中,使用最新的Swift3如何实现呢?下面通过实际代码介绍。

设置Storyboard

Storyboard的布局非常简单,一个UIImageView,两个Button分别是从摄像头获取图片和从相册获取图片。

设置

初始化

开始前需要给ViewController添加两个delegates,然后绑定UIImageView,添加一个UIImagePickerController的实例,代码如下:

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    @IBOutlet var imageView: UIImageView!
    
    let imagePicker = UIImagePickerController()

其次,还需要设置我们的ViewController作为UIImagePickerController的delegate:

override func viewDidLoad() {
    super.viewDidLoad()

    imagePicker.delegate = self
}

通过摄像头获取图片

通过上面的设置和初始化,接下来就可以通过iPhone的摄像头获取图片了,我们将其封装在一个函数里面:

    func openCamera(){
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {

            imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
            imagePicker.allowsEditing = true
            self.present(imagePicker, animated: true, completion: nil)
        }
        
    }
通过摄像头获取图片

从相册中获取图片

从相册中获取图片的方法和通过摄像头获取图片的方法非常类似,代码如下:

    func openPhoto(){
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
            
            imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
            imagePicker.allowsEditing = true
            self.present(imagePicker, animated: true, completion: nil)
        }
    }
从相册中获取图片

显示获取到的图片

通过以上两个方法,我们获取到了想要的图片,最后就是将其显示在UIImageView中,代码如下:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        imageView.image = info[UIImagePickerControllerEditedImage] as! UIImage
        self.dismiss(animated: true, completion: nil)
    }

imagePickerController是一个回调方法,当我们选择好图片之后,系统会调用这个方法,info是一个字典,UIImagePickerControllerEditedImage这个key值对应的value就是取到的编辑后的图片,为UIImage类型。

显示获取到的图片

附:didFinishPickingMediaWithInfo字典对应的key:value。

let UIImagePickerControllerMediaType: String

Specifies the media type selected by the user.

let UIImagePickerControllerOriginalImage: String

Specifies the original, uncropped image selected by the user.

let UIImagePickerControllerEditedImage: String

Specifies an image edited by the user.

let UIImagePickerControllerCropRect: String

Specifies the cropping rectangle that was applied to the original image.

let UIImagePickerControllerMediaURL: String

Specifies the filesystem URL for the movie.

let UIImagePickerControllerReferenceURL: String

The Assets Library URL for the original version of the picked item.

let UIImagePickerControllerMediaMetadata: String

Metadata for a newly-captured photograph.

let UIImagePickerControllerLivePhoto: String

The Live Photo representation of the selected or captured photo.

本文作者:小池laucherish
本文出处:http://www.jianshu.com/p/302a632950a0
转载请在开头注明作者详细信息本文出处

相关文章

网友评论

    本文标题:[iOS]iOS10中通过摄像头或者从相册中获取图片的方法

    本文链接:https://www.haomeiwen.com/subject/whmibttx.html