Swift—调用系统相册和相机

时间:2021-10-02 06:16:59

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #000000 }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #000000; min-height: 28.0px }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #294c50 }
p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #c81b13 }
p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #703daa }
p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #539aa4 }
span.s1 { color: #c42275 }
span.s2 { }
span.s3 { color: #6122ae }
span.s4 { color: #703daa }
span.s5 { color: #000000 }
span.s6 { color: #3e1e81 }
span.s7 { color: #539aa4 }
span.s8 { color: #0435ff }
span.s9 { color: #c81b13 }
span.s10 { font: 24.0px "PingFang SC"; color: #c81b13 }

//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)

}

}

//控制器界面代码

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #000000 }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #000000; min-height: 28.0px }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #6122ae }
p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #703daa }
p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #3e1e81 }
p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #1e9421 }
p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #1e9421; min-height: 28.0px }
span.s1 { color: #c42275 }
span.s2 { }
span.s3 { color: #000000 }
span.s4 { color: #3c828b }
span.s5 { color: #3e1e81 }
span.s6 { color: #6122ae }
span.s7 { color: #539aa4 }
span.s8 { color: #703daa }
span.s9 { color: #294c50 }
span.s10 { color: #1e9421 }

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.

}

}

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

Swift—调用系统相册和相机