swift 使用UIIMagePicker轻松选择图像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 使用UIIMagePicker轻松选择图像相关的知识,希望对你有一定的参考价值。

//
//  ImagePickerHelper.swift
//  NeverMynd
//
//  Created by Nirav Joshi on 14/06/18.
//  Copyright © 2018 DEVSTREE. All rights reserved.
//

import Foundation

protocol ImagePickerPresentable: class {
    func showImagePicker(allowsEditing:Bool, source: [UIImagePickerControllerSourceType])
    func selectedImage(image: UIImage?)
}

fileprivate class ImagePickerHelper: NSObject {
    
    weak var delegate: ImagePickerPresentable?
    
    fileprivate struct `Static` {
        fileprivate static var instance: ImagePickerHelper?
    }
    
    fileprivate class var shared: ImagePickerHelper {
        if ImagePickerHelper.Static.instance == nil {
            ImagePickerHelper.Static.instance = ImagePickerHelper()
        }
        return ImagePickerHelper.Static.instance!
    }
    
    fileprivate func dispose() {
        ImagePickerHelper.Static.instance = nil
    }
    
    func picker(picker: UIImagePickerController, selectedImage image: UIImage?) {
        picker.dismiss(animated: true, completion: nil)
        self.delegate?.selectedImage(image: image)
        self.delegate = nil
        self.dispose()
    }
}

extension ImagePickerHelper: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        self.picker(picker: picker, selectedImage: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        guard var image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
            return self.picker(picker: picker, selectedImage: nil)
        }
        
        if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
            image = editedImage
        }
        
        self.picker(picker: picker, selectedImage: image)
    }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
        self.picker(picker: picker, selectedImage: image)
    }
}

extension ImagePickerPresentable where Self: UIViewController {
    
    fileprivate func pickerControllerActionFor(for type: UIImagePickerControllerSourceType, title: String, allowsEditing:Bool) -> UIAlertAction? {
        guard UIImagePickerController.isSourceTypeAvailable(type) else {
            return nil
        }
        return UIAlertAction(title: title, style: .default) { [unowned self] _ in
            let pickerController           = UIImagePickerController()
            pickerController.delegate      = ImagePickerHelper.shared
            pickerController.sourceType    = type
            pickerController.allowsEditing = allowsEditing
            self.present(pickerController, animated: true)
        }
    }
    
    func showImagePicker(allowsEditing:Bool, source: [UIImagePickerControllerSourceType]) {
        ImagePickerHelper.shared.delegate = self
        
        let optionMenu = UIAlertController(title: "Select image an option", message: nil, preferredStyle: .alert)
        
        if source.count == 1 {
            
            if !UIImagePickerController.isSourceTypeAvailable(source.first!) {
                self.showAlertMessage("Camera not available in your decive")
                return
            }
            
            let pickerController           = UIImagePickerController()
            pickerController.delegate      = ImagePickerHelper.shared
            pickerController.sourceType    = source.first!
            pickerController.allowsEditing = allowsEditing
            self.present(pickerController, animated: true)
            return
        }
        
        if source.contains(.camera) {
            if let action = self.pickerControllerActionFor(for: .camera, title: "Take photo", allowsEditing: allowsEditing) {
                optionMenu.addAction(action)
            }
        }
        
        if source.contains(.photoLibrary) {
            if let action = self.pickerControllerActionFor(for: .photoLibrary, title: "Select from library", allowsEditing: allowsEditing) {
                optionMenu.addAction(action)
            }
        }
        
        
        optionMenu.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        
        self.present(optionMenu, animated: true)
    }
}

以上是关于swift 使用UIIMagePicker轻松选择图像的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Documents 文件夹中保存 UIImagePicker 中的 UIImage 并获取路径字符串以保存在 Swift 中的 Core Data 中

UIImagePicker 选择后崩溃

iPad 问题:使用 UIImagePicker 从视频文件夹中选择视频

从swift库中选择照片不起作用

UIPopoverController 中的 iPad UIImagePicker 仅选择保存的图像(不是来自相册)?

从 uiimagepicker 上传 uiwebview 中的图像