目的: 代码简洁,当我们在控制器中或是需要调用的时候,通常的办法是
1.创建UIImagePickerController的对象,并实现它的代理方法
2.在相应的代理方法中获取我们在手机中的相册的图片,或是调用摄像头拍摄图片并获取图片。
弊端:
1.需要的代码量比较多
2.不利于代码的复用(当多个地方需要调用的时候)
解决办法:
1.把需要UIImagePickerController的对象和相应的代理方法放在一个UIController的扩展文件中
2.需要区别你要调用的是相册还是摄像头,刚好苹果API中有相应的属性:
UIImagePickerControllerSourceType.camera.rawValue,
UIImagePickerControllerSourceType.photoLibrary.rawValue
3.当然我们又时候还可能需要用到图片的类型,是可编辑还是原始的图片,这个也有属性:
UIImagePickerControllerEditedImage,
UIImagePickerControllerOriginalImage
4.在获取图片的代理方法中获得最终需要的图片,并在你的控制器中重写这个方法
难点:
在扩展中我们需要定义我们的属性,用来存储我们调用的类型和图片的类型,这时候我们会用到属性关联
public protocol PropertyStoring {
associatedtype T
associatedtype AlbumT
func getAssociatedObject(_ key: UnsafeRawPointer!, defaultValue: T) -> T
func getAssociatedObject(_ key: UnsafeRawPointer!, defaultValue: AlbumT) -> AlbumT
}
public extension PropertyStoring {
func getAssociatedObject(_ key: UnsafeRawPointer!, defaultValue: T) -> T {
guard let value = objc_getAssociatedObject(self, key) as? T else {
return defaultValue
}
return value
}
func getAssociatedObject(_ key: UnsafeRawPointer!, defaultValue: AlbumT) -> AlbumT {
guard let value = objc_getAssociatedObject(self, key) as? AlbumT else {
return defaultValue
}
return value
}
}
这一段代码是扩展类中属性关联的写法,首先是定义一个协议并有相应的关联类型和方法,之后我们需要实现该协议中的方法,把属性关联上。
extension UIViewController:UIImagePickerControllerDelegate, UINavigationControllerDelegate, PropertyStoring {
public typealias T = String
public typealias AlbumT = Int
private struct CustomProperties {
static var imgType = UIImagePickerControllerOriginalImage
static var isAlbum = UIImagePickerControllerSourceType.photoLibrary
}
var imgType: String {
get {
return getAssociatedObject(&CustomProperties.imgType, defaultValue: CustomProperties.imgType)
}
set {
return objc_setAssociatedObject(self, &CustomProperties.imgType, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
var albumType: Int {
get {
return getAssociatedObject(&CustomProperties.isAlbum, defaultValue: CustomProperties.isAlbum.rawValue)
}
set {
return objc_setAssociatedObject(self, &CustomProperties.isAlbum, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
func invokingSystemAlbumOrCamera(type: String, albumT: Int) -> Void {
self.imgType = type
self.albumType = albumT
if albumT == UIImagePickerControllerSourceType.photoLibrary.rawValue {
self.invokeSystemPhoto()
}else {
self.invokeSystemCamera()
}
}
func invokeSystemPhoto() -> Void {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = .photoLibrary
imagePickerController.delegate = self
imagePickerController.allowsEditing = false
if self.imgType == UIImagePickerControllerEditedImage {
imagePickerController.allowsEditing = true
}else {
imagePickerController.allowsEditing = false
}
if #available(iOS 11.0, *) {
UIScrollView.appearance().contentInsetAdjustmentBehavior = .automatic
}
self.present(imagePickerController, animated: true, completion: nil)
}else {
print("请打开允许访问相册权限")
}
}
func invokeSystemCamera() -> Void {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = .camera
imagePickerController.delegate = self
imagePickerController.allowsEditing = false
imagePickerController.cameraCaptureMode = .photo
imagePickerController.mediaTypes = ["public.image"]
self.imgType = UIImagePickerControllerOriginalImage
if #available(iOS 11.0, *) {
UIScrollView.appearance().contentInsetAdjustmentBehavior = .automatic
}
self.present(imagePickerController, animated: true, completion: nil)
}else {
print("请打开允许访问相机权限")
}
}
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if #available(iOS 11.0, *) {
UIScrollView.appearance().contentInsetAdjustmentBehavior = .never
}
picker.dismiss(animated: true, completion: nil)
let img = info[self.imgType] as! UIImage
if self.albumType == UIImagePickerControllerSourceType.photoLibrary.rawValue {
self.reloadViewWithImg(img: img)
}else {
self.reloadViewWithCameraImg(img: img)
}
}
@objc func reloadViewWithImg(img: UIImage) -> Void {
}
@objc func reloadViewWithCameraImg(img: UIImage) -> Void {
}
public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
if #available(iOS 11.0, *) {
UIScrollView.appearance().contentInsetAdjustmentBehavior = .never
}
self.dismiss(animated: true, completion: nil)
}
}
在这段代码中首先是定义我们会用的属性,并写上属性的set和get方法,这样我们就可以给属性赋值了。
当然对外的方法就是:
func invokingSystemAlbumOrCamera(type: String, albumT: Int)
在你需要用到的控制器中调用:
self.invokingSystemAlbumOrCamera(type: UIImagePickerControllerOriginalImage,
albumT: UIImagePickerControllerSourceType.camera.rawValue)
接着就是怎样把需要的图片在控制器获取到
比如你可能会在UserViewController中会用到:
override func reloadViewWithImg(img: UIImage) {
uploadImg(img: img)
}
override func reloadViewWithCameraImg(img: UIImage) {
uploadImg(img: img)
}
这样我们就可以获取到了图片了。
好了,这样我们就可以非常简洁的调用系统图片或是摄像头获取我们的图片。
网友评论