Last active
September 6, 2017 09:55
-
-
Save K-Kevin/7cea70b8b189338da107e929cfd00268 to your computer and use it in GitHub Desktop.
Access to Camera and Photo Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import Photos | |
import AVFoundation | |
import MediaCenterSDK | |
import Kingfisher | |
import NVActivityIndicatorView | |
import ObjectMapper | |
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, NVActivityIndicatorViewable { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
self.title = "上传图片<单张>" | |
self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) | |
self.view.addSubview(self.uploadBtn) | |
self.view.addSubview(self.introLabel) | |
self.view.addSubview(self.pickedImageView) | |
NVActivityIndicatorView.DEFAULT_BLOCKER_MESSAGE_FONT = UIFont.boldSystemFont(ofSize: 14) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
// MARK: - UIImagePickerControllerDelegate | |
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { | |
picker.dismiss(animated: true, completion: nil) | |
} | |
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { | |
var image:UIImage? = nil | |
if picker.allowsEditing { | |
image = info["UIImagePickerControllerEditedImage"] as? UIImage | |
} else { | |
image = info["UIImagePickerControllerOriginalImage"] as? UIImage | |
} | |
} | |
// MARK: - custom method | |
/// 选择照片 | |
func selectPhoto() { | |
let alertVC = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet) | |
let photoAction = UIAlertAction.init(title: "从相册选择", style: .default) { (action) in | |
self.changeAvatarWithType(isCamera: false) | |
} | |
alertVC.addAction(photoAction) | |
let cameraAction = UIAlertAction.init(title: "拍照", style: .default) { (action) in | |
self.changeAvatarWithType(isCamera: true) | |
} | |
alertVC.addAction(cameraAction) | |
let cancelAction = UIAlertAction.init(title: "取消", style: .cancel) { (action) in | |
} | |
alertVC.addAction(cancelAction) | |
self.present(alertVC, animated: true, completion: nil) | |
} | |
func changeAvatarWithType(isCamera:Bool) { | |
if isCamera { | |
cameraAuthorization() | |
} else { | |
assetsAuthorization() | |
} | |
} | |
func assetsAuthorization() { | |
let status = PHPhotoLibrary.authorizationStatus() | |
if status == .notDetermined { | |
PHPhotoLibrary.requestAuthorization({ (status) in | |
if status == .authorized { | |
self.avatarChangeWithType(isCamera:false) | |
} | |
}) | |
} else if status == .authorized { | |
self.avatarChangeWithType(isCamera:false) | |
} else { | |
let alertVC = UIAlertController.init(title: "此应用没有权限访问您的照片或视频", message: "您可以在”隐私设置“中启动访问", preferredStyle: .alert) | |
let setAction = UIAlertAction.init(title: "设置", style: .default, handler: { (action) in | |
UIApplication.shared.openURL(NSURL(string:UIApplicationOpenSettingsURLString)! as URL) | |
}) | |
alertVC.addAction(setAction) | |
let cancelAction = UIAlertAction.init(title: "取消", style: .default, handler: { (action) in | |
}) | |
alertVC.addAction(cancelAction) | |
} | |
} | |
func cameraAuthorization() { | |
let status = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) | |
if status == .authorized { | |
self.avatarChangeWithType(isCamera:true) | |
} else if status == .notDetermined { | |
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (granted) in | |
if granted { | |
self.avatarChangeWithType(isCamera:true) | |
} | |
}) | |
} else if status == .denied || status == .restricted { | |
let alertVC = UIAlertController.init(title: "此应用没有权限访问您的相机", message: "您可以在”隐私设置“中启动访问", preferredStyle: .alert) | |
let setAction = UIAlertAction.init(title: "设置", style: .default, handler: { (action) in | |
UIApplication.shared.openURL(NSURL(string:UIApplicationOpenSettingsURLString)! as URL) | |
}) | |
alertVC.addAction(setAction) | |
let cancelAction = UIAlertAction.init(title: "取消", style: .default, handler: { (action) in | |
}) | |
alertVC.addAction(cancelAction) | |
} | |
} | |
func avatarChangeWithType(isCamera:Bool) { | |
let imagePickerVC = UIImagePickerController() | |
if isCamera { | |
if UIImagePickerController.isSourceTypeAvailable(.camera) { | |
imagePickerVC.sourceType = .camera | |
} | |
} else { | |
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) { | |
imagePickerVC.sourceType = .photoLibrary | |
} | |
} | |
//imagePickerVC.allowsEditing = true | |
imagePickerVC.delegate = self | |
self.present(imagePickerVC, animated: true, completion: nil) | |
} | |
// MARK: - lazy load | |
fileprivate lazy var uploadBtn: UIButton = { | |
let button = UIButton.init(type: .system) | |
button.frame = CGRect(x: 20, y: 120, width: UIScreen.main.bounds.size.width - 40, height: 40) | |
button.setTitle("点击选择照片上传", for: .normal) | |
button.layer.cornerRadius = 4 | |
button.layer.borderWidth = 0.5 | |
button.layer.borderColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1).cgColor | |
button.addTarget(self, action: #selector(selectPhoto), for: .touchUpInside) | |
return button | |
}() | |
fileprivate lazy var pickedImageView: UIImageView = { | |
let imageView = UIImageView.init() | |
imageView.clipsToBounds = true | |
imageView.contentMode = .scaleAspectFill | |
imageView.frame = CGRect(x: 20, y: 180, width: UIScreen.main.bounds.size.width - 40, height: UIScreen.main.bounds.size.width - 40) | |
return imageView | |
}() | |
fileprivate lazy var introLabel: UILabel = { | |
let label = UILabel.init() | |
label.frame = CGRect(x: 20, y: 70, width: UIScreen.main.bounds.size.width - 40, height:40) | |
label.numberOfLines = 0 | |
label.textColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1) | |
label.font = UIFont.systemFont(ofSize: 14) | |
return label | |
}() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
class CameraHandler: NSObject{ | |
static let shared = CameraHandler() | |
fileprivate var currentVC: UIViewController! | |
//MARK: Internal Properties | |
var imagePickedBlock: ((UIImage) -> Void)? | |
func camera() | |
{ | |
if UIImagePickerController.isSourceTypeAvailable(.camera){ | |
let myPickerController = UIImagePickerController() | |
myPickerController.delegate = self; | |
myPickerController.sourceType = .camera | |
currentVC.present(myPickerController, animated: true, completion: nil) | |
} | |
} | |
func photoLibrary() | |
{ | |
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){ | |
let myPickerController = UIImagePickerController() | |
myPickerController.delegate = self; | |
myPickerController.sourceType = .photoLibrary | |
currentVC.present(myPickerController, animated: true, completion: nil) | |
} | |
} | |
func showActionSheet(vc: UIViewController) { | |
currentVC = vc | |
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) | |
actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (alert:UIAlertAction!) -> Void in | |
self.camera() | |
})) | |
actionSheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (alert:UIAlertAction!) -> Void in | |
self.photoLibrary() | |
})) | |
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) | |
vc.present(actionSheet, animated: true, completion: nil) | |
} | |
} | |
extension CameraHandler: UIImagePickerControllerDelegate, UINavigationControllerDelegate{ | |
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { | |
currentVC.dismiss(animated: true, completion: nil) | |
} | |
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { | |
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { | |
self.imagePickedBlock?(image) | |
}else{ | |
print("Something went wrong") | |
} | |
currentVC.dismiss(animated: true, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment