欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-04-11 16:11:33
...

一、创建项目

这个很简单,创建好了看下面的就行了

 

二、添加权限

拍照和获取相册都需要先设置权限

 

三、布局

实现一个简单的布局,同一个界面,一个按钮 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

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

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