Swift 5.0 获取图片-相册、拍照

2021-06-09  本文已影响0人  光光6

一、准备工作: 

创建项目 ,创建一个空白项目,好了看下面的就行了

二、相关权限的添加:拍照和获取相册都需要先设置权限

权限添加注意点:info.plist中添加两个keyNSPhotoLibraryUsageDescription,和NSCameraUsageDescription,填写使用的提示内容.

三、在新项目里ViewController,写一个简单的布局

实例化一个按钮 Button,一张图片 ImageView

四、代码的具体实现

1、 首先、要继承代理 UIImagePickerControllerDelegate, UINavigationControllerDelegate

2、拍照或从相册选择图片都是通过 UIImagePickerController.init()     

    设置 sourceType = .camera 是拍照

    设置 sourceType = .photoLibrary 是从相册选择图片

3、相册选择或拍照都是通过 imagePickerController 方法返回,如果对图片还要做处理也可以在这里做

import UIKit

class ImageController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{

    //图片展示

    @IBOutlet weak var image: UIImageView!

    var takingPicture:UIImagePickerController!

    //点击按钮弹出拍照、相册的选择框

    @IBAction func getImage(_ sender: Any) {

        let actionSheetController = UIAlertController()

        let cancelAction = UIAlertAction(title: "取消", style: UIAlertAction.Style.cancel) { (alertAction) -> Void in

            print("Tap 取消 Button")

        }

        let takingPicturesAction = UIAlertAction(title: "拍照", style: UIAlertAction.Style.destructive) { (alertAction) -> Void in

            self.getImageGo(type: 1)

        }

        let photoAlbumAction = UIAlertAction(title: "相册", style: UIAlertAction.Style.default) { (alertAction) -> Void in

            self.getImageGo(type: 2)

        }

        actionSheetController.addAction(cancelAction)

        actionSheetController.addAction(takingPicturesAction)

        actionSheetController.addAction(photoAlbumAction)

        //iPad设备浮动层设置锚点

        actionSheetController.popoverPresentationController?.sourceView = sender as? UIView

        //显示

        self.present(actionSheetController, animated: true, completion: nil)

    }

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

    //去拍照或者去相册选择图片

    func getImageGo(type:Int){

        takingPicture =  UIImagePickerController.init()

        if(type==1){

            takingPicture.sourceType = .camera

            //拍照时是否显示工具栏

            //takingPicture.showsCameraControls = true

        }else if(type==2){

            takingPicture.sourceType = .photoLibrary

        }

        //是否截取,设置为true在获取图片后可以将其截取成正方形

        takingPicture.allowsEditing = false

        takingPicture.delegate = self

        present(takingPicture, animated: true, completion: nil)

    }

    //拍照或是相册选择返回的图片

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        takingPicture.dismiss(animated: true, completion: nil)

        if(takingPicture.allowsEditing == false){

            //原图

            image.image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage

        }else{

            //截图

            image.image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage

        }

    }

}

五、设置相册语言环境

上面功能已经实现,简单拍照,图片获取实现了;

但是,你会发现,选择拍照时,上面的操作步骤文字都是英文的,我们可以把它改成中文

如下图所示,选中项目 -> PEOJECT -> Info -> Localization

点击 + 将 China,Simplified 添加到 Localization 重新运行项目就可以看到拍照界面都是中文了

info.plist设置
上一篇 下一篇

猜你喜欢

热点阅读