• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift—调用系统相册和相机

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

//UI界面代码

import UIKit

 

class CameraView: UIView {

    var cameraButton : UIButton!

    var photoButton : UIButton!

    var image : UIImageView!

    override init(frame : CGRect)

    {

        super.init(frame: frame)

        cameraButtonMethods()

        photoButtonMethods()

        imageMethods()

    }

    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    func cameraButtonMethods(){

        

        cameraButton = UIButton(type: .custom)

        cameraButton.frame = CGRect(x: 20, y: 400, width: 100, height: 40)

        cameraButton.setTitle("调用相册", for: .normal)

        cameraButton.backgroundColor = UIColor.blue

        self.addSubview(cameraButton)

    

    }

    func photoButtonMethods(){

         photoButton = UIButton(type: .custom)

         photoButton.frame = CGRect(x: 255, y: 400, width: 100, height: 40)

         photoButton.backgroundColor = UIColor.blue

         photoButton.setTitle("调用相机", for: .normal)

        self.addSubview(photoButton)

        

    }

    func imageMethods(){

         image = UIImageView()

         image.image = UIImage(named: "image")

         image.frame = CGRect(x: 20, y: 40, width: 335, height: 335)

         self.addSubview(image)

    }

 

}

 

 

//控制器界面代码

import UIKit

 

class CameraViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    var cameraView = CameraView()

    override func viewDidLoad() {

        super.viewDidLoad()

 

        self.view.backgroundColor = UIColor.white

        cameraView = CameraView(frame: UIScreen.main.bounds)

        self.view.addSubview(cameraView)

        cameraView.cameraButton.addTarget(self, action: #selector(CameraViewController.cameraEvent), for: .touchUpInside)

        cameraView.photoButton.addTarget(self, action: #selector(CameraViewController.photoEvent), for: .touchUpInside)

    }

    func cameraEvent(){

        let pickerCamera = UIImagePickerController()

        pickerCamera.delegate = self

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

    }

    func photoEvent(){

        

        let pickerPhoto = UIImagePickerController()

        pickerPhoto.sourceType = .camera

        pickerPhoto.delegate = self

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

    

    }

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

        

         let imagePickerc = info[UIImagePickerControllerOriginalImage] as! UIImage

        cameraView.image.image = imagePickerc

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

       

    }

 

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

 

 

}

 

//调用系统相册与相机时的配置,不然调取不会成功

 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
iOS之Swift语言的学习发布时间:2022-07-13
下一篇:
Openstack swift对象存储发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap