In Swift 3.0 in questo codice si dispone di selezionare sia della macchina fotografica di opzione e Gallary
import UIKit
import Foundation
import AVFoundation
class HostVC: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate{
@IBOutlet weak var imgHostProfile:UIImageView!
let captureSession = AVCaptureSession()
let stillImageOutput = AVCaptureStillImageOutput()
var previewLayer : AVCaptureVideoPreviewLayer?
var captureDevice : AVCaptureDevice?
@IBAction func btnChangeprofileTapped(_ sender: UIButton)
{
DispatchQueue.main.async
{
let alert = UIAlertController(title: "Alert", message: "Choose profile picture from Camera or Gallery", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Take a photo", style: UIAlertActionStyle.default, handler: { (action: UIAlertAction!) in
DispatchQueue.main.async
{
if AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) == AVAuthorizationStatus.authorized {
self.Cemara()
} else {
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
if granted == true {
self.Cemara()
} else {
self.showalert(strMessage: Validation.kCameraAccess)
}
})
}
}
}))
alert.addAction(UIAlertAction(title: "Choose from Library", style: UIAlertActionStyle.default, handler: { (action: UIAlertAction!) in
DispatchQueue.main.async
{
let imagepicker = UIImagePickerController()
imagepicker.delegate = self
imagepicker.sourceType = .photoLibrary
self.present(imagepicker, animated: true, completion: nil)
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: { (action: UIAlertAction!) in}))
self.present(alert, animated: true, completion: nil)
}
}
func Cemara()
{
DispatchQueue.main.async {
if UIImagePickerController.availableCaptureModes(for: .rear) != nil
{
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
self.present(imagePicker, animated: true, completion: nil)
}
else
{
self.noCamera()
}
}
}
//======================================================================
// MARK: - Camera Code
//======================================================================
func noCamera()
{
captureSession.sessionPreset = AVCaptureSessionPresetHigh
if let devices = AVCaptureDevice.devices() as? [AVCaptureDevice]
{
for device in devices
{
if (device.hasMediaType(AVMediaTypeVideo))
{
if(device.position == AVCaptureDevicePosition.front)
{
captureDevice = device
if captureDevice != nil
{
print("Capture device found")
beginSession()
}
}
}
}
}
}
//======================================================================
// MARK: - Camera Capture Start Session Code Here
//======================================================================
func beginSession()
{
do
{
try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice))
stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
if captureSession.canAddOutput(stillImageOutput)
{
captureSession.addOutput(stillImageOutput)
}
}
catch
{
print("error: \(error.localizedDescription)")
}
guard let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) else
{
print("no preview layer")
return
}
self.view.layer.addSublayer(previewLayer)
previewLayer.frame = self.view.layer.frame
captureSession.startRunning()
self.view.addSubview(imgHostProfile)
}
//======================================================================
// MARK: - Gallary Code
//======================================================================
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
if let imagedata = info[UIImagePickerControllerOriginalImage] as? UIImage
{
self.imgHostProfile.image = imagedata
print(imagedata)
}
dismiss(animated: true, completion: nil)
}
fonte
2017-11-08 04:54:56
@adnan non ha funzionato, sono l'aggiunta del imageChosen all'interno dello stesso avviso, self.view –
@adnan va bene, sto controllando ora –
@adnan ho visto questo codice qui http://www.theappguruz.com/ blog/user-interaction-camera-using-uiimagepickercontroller-swift /, tutto ciò che voglio è mettere l'immagine in imageView, senza creare i pulsanti, grazie –