Swift-calling system albums and cameras

Source: Internet
Author: User
Tags uikit

UI Interface Code

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:) have not been implemented")

}

func camerabuttonmethods () {

Camerabutton = UIButton(type:. Custom)

Camerabutton. Frame = CGRect(x: y: +, width: + , Height: + )

Camerabutton. Settitle(" call album ", for:. ) Normal)

Camerabutton. BackgroundColor = uicolor. Blue

self. Addsubview(camerabutton)

}

func photobuttonmethods () {

Photobutton = UIButton(type:. Custom)

Photobutton. Frame = CGRect(x: 255, y: max, Width: +, Height: +)

Photobutton. BackgroundColor = uicolor. Blue

Photobutton. Settitle(" call camera ", for:. ) Normal)

self. Addsubview(photobutton)

}

func imagemethods () {

image = uiimageview()

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

image. Frame = CGRect(x: y: + , Width: 335, Height: 335)

self. Addsubview(image)

}

}

Controller Interface Code

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, can be recreated.

}

}

Call the system album with the camera when the configuration, otherwise the tuning will not be successful

Swift-calling system albums and cameras

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.