UIImagePicker初始化花费的时间太长

时间:2020-05-15 18:57:48

标签: ios

我有一个imagePicker,当我初始化它时,它花了很长时间,它使屏幕滞后,例如,当我单击一个图像时,我有一个屏幕,用户可以在其中写信息并选择图片。按钮以移至该屏幕,它会滞后于实际移至该屏幕。 我运行了Time Profiler,屏幕问题似乎是imagePicker的初始化。

这是imagePicker类:​​

import UIKit

public protocol ImagePickerDelegate: class {
    func didSelect(image: UIImage?)
}

class ImagePicker: NSObject {

    private let pickerController: UIImagePickerController
    private weak var presentationController: UIViewController?
    private weak var delegate: ImagePickerDelegate?

    init(presentationController: UIViewController, delegate: ImagePickerDelegate){
        self.pickerController = UIImagePickerController()

        super.init()

        self.presentationController = presentationController
        self.delegate = delegate

        self.pickerController.delegate = self
        self.pickerController.allowsEditing = true
        self.pickerController.mediaTypes = ["public.image"]
    }

    private func action(for type: UIImagePickerController.SourceType, title: String) -> UIAlertAction?{
        guard UIImagePickerController.isSourceTypeAvailable(type) else { return nil}
        return UIAlertAction(title: title, style: .default, handler: { [unowned self] _ in
            self.pickerController.sourceType = type
            self.presentationController?.present(self.pickerController, animated: true)
        })
    }

    func present(from sourceView: UIView){
        let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        if let action = self.action(for: .camera, title: ImagePickerStrings.takePicture){
            alertController.addAction(action)
        }

        if let action = self.action(for: .savedPhotosAlbum, title: ImagePickerStrings.cameraRoll) {
            alertController.addAction(action)
        }

        alertController.addAction(UIAlertAction(title: GeneralStrings.cancel, style: .cancel, handler: nil))

        self.presentationController?.present(alertController, animated: true)
    }

    private func pickerController(_ controller: UIImagePickerController, didSelect image: UIImage?){
        controller.dismiss(animated: true, completion: nil)
        self.delegate?.didSelect(image: image)
    }
}

extension ImagePicker: UIImagePickerControllerDelegate{
    public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        self.pickerController(picker, didSelect: nil)
    }

    public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let image = info[.editedImage] as? UIImage else {
            return self.pickerController(picker, didSelect: nil)
        }
        self.pickerController(picker, didSelect: image)
    }
}

这是我初始化它的方式:

我在班级里有这个变量:

var imagePicker: ImagePicker!

这是在viewDidLoad中:

override func viewDidLoad() {
    super.viewDidLoad()
    imagePicker = ImagePicker(presentationController: self, delegate: self)
}

1 个答案:

答案 0 :(得分:1)

在模拟器上通常很慢,而在调试模式下总体来说很慢。

请参见UIImagePickerController really slow when calling alloc init