swift使用相机拍照
Posted hequnwang10
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift使用相机拍照相关的知识,希望对你有一定的参考价值。
这篇教程介绍如何在应用程序中进行拍摄照片,并将照片添加到APP中。
创建一个UIkit的框架项目
一、info.list
申请权限
Privacy - Camera Usage Description
二、代码
//
// ViewController.swift
// TakingPictures
//
// Created by whq on 2021/8/15.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet var imageView : UIImageView!
@IBOutlet var button : UIButton!
override func viewDidLoad() {
super.viewDidLoad()
imageView.backgroundColor = .secondarySystemBackground
button.backgroundColor = .systemBlue
button.setTitle("Take picture", for: .normal)
button.setTitleColor(.white, for: .normal)
}
@IBAction func didTapButton(){
let picker = UIImagePickerController()
picker.sourceType = .camera
picker.allowsEditing = true
picker.delegate = self
present(picker, animated: true)
}
}
extension ViewController :UIImagePickerControllerDelegate,UINavigationControllerDelegate{
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
guard let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage else{
return
}
imageView.image = image
}
}
三、main.storyboard
自行绑定
这样效果就出来了!
以上是关于swift使用相机拍照的主要内容,如果未能解决你的问题,请参考以下文章