iOS swift中UIImagePickerControllerDelegate代理不执行问题

语哥 发布于 2017/02/23 15:09
阅读 396
收藏 0

///代码块1

import UIKit
import MobileCoreServices
import AssetsLibrary

/// 检查设备是否支持相机录制
///
/// - Returns: true or false
public func isSupportCameraRecording() -> Bool {
    guard let avTypes: [String] = UIImagePickerController.availableMediaTypes(for: .camera) else {
        return false
    }
    return avTypes.contains(kUTTypeMovie as String)
}

open class cameraVC: NSObject,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    
    public var cameraVC: UIImagePickerController?
    
    /// 
    public var setCameraDeviceFront: Bool = false
    
    override init() {
        super.init()
        cameraVC = setupImagePicker()
    }    
    
    private func setupImagePicker() -> UIImagePickerController {
        let camera = UIImagePickerController()
            camera.sourceType = UIImagePickerControllerSourceType.camera
            camera.mediaTypes = [kUTTypeMovie as String]
            camera.videoQuality = .typeHigh
            camera.delegate = self
            setCameraDeviceFront(picker: camera)
//            configureCustomUI(picker: camera)
        return camera
    }
    
    private func setCameraDeviceFront(picker: UIImagePickerController) {
        
        let defCameraDeviceFront = UIImagePickerController.isCameraDeviceAvailable(.rear)
        if setCameraDeviceFront && defCameraDeviceFront {
            picker.cameraDevice = .front
        }
    }
    
    private func configureCustomUI(picker: UIImagePickerController) {
        let cameraOverlay = UIView()
        picker.showsCameraControls = false
        picker.cameraOverlayView = cameraOverlay
    }
    
//MARK:            delegate
    public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
    
    public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let library = ALAssetsLibrary()
        let url: URL = info["UIImagePickerControllerMediaURL"] as! URL
        if library.videoAtPathIs(compatibleWithSavedPhotosAlbum: url) {
            library.writeVideoAtPath(toSavedPhotosAlbum: url, completionBlock: { ( _, _)in})
        }
        picker.dismiss(animated: true, completion: nil)
    }
}

/// 调用的地方 控制器里面
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if !isSupportCameraRecording() {
            return
        }
        let camera = cameraVC()
/// 如果 camera.cameraVC.delegate = self 写在这里
        present(camera.cameraVC!, animated: true, completion: nil)
    }
/// 并执行代理方法就会执行,而同样的代码Oc里面不会存在这种情况

如上代码中在相机录制结束后或者点击Cancle 是没有执行代理方法的,而如果把代理方法写在调用的地方,且实现代理方法就会执行,这是什么原因?一样的代码Oc里面却没问题?

 

 

 

加载中
返回顶部
顶部