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

一、创建项目

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

 

二、添加权限

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

 

三、布局

实现一个简单的布局,同一个界面,一个按钮 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 重新运行项目就可以看到拍照界面都是中文了

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值