为什么imagePickerController.mediaTypes = [kUTTypeMovie as String]包括实时照片作为视频的反弹或循环效果?

时间:2018-03-21 18:06:13

标签: ios swift uiimagepickercontroller avplayer uiimagepickercontrollermediatype

我使用UIImagePicker只从我的库中挑选视频,并将媒体类型设置为:

imagePicker.mediaTypes = [kUTTypeMovie as String]

我注意到,当图书馆出现时,即使我只期待视频,如果图片库中有实时照片,但BounceLoop效果也会显示。如果我拍摄同一张照片并将其效果切换为LiveLong Exposure,则它将不再显示在库中。如果我将其重新切换回BounceLoop,则会重新显示。

我已经尝试了.photoLibrary.savedPhotosAlbum,同样的事情发生了。

我问的原因是因为我想让他们不在图书馆里,而我只想要挑选xxx秒或更多的视频。

  1. 为什么这些效果类型会显示在图片选择器库中,即使我只选择视频?
  2. 为什么只显示LoopBounce,但Long ExposureLive没有?
  3. 如何阻止他们?
  4. 也许这是一个单独的问题,但由于它与相同的LiveBounce照片问题相关,我添加了它。我注意到如果我在AVPlayer中放置一个来自LiveBounce照片的网址,如果我转到后台然后回来,当应用重新进入前景,它只发生在那两个效果(不应该是可用的)。当应用程序进入后台时,我使用此方法Remove AVPlayerLayer正确删除了AVPlayerLayer,但是当在播放器内部初始化效果类型的url时,它仍在前台暂时冻结(app无响应)

    代码:

    import MobileCoreServices
    import AVFoundation
    
    class ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
    
    var playerItem: AVPlayerItem?
    var player: AVPlayer?
    var playerLayer: AVPlayerLayer?
    var currentPlayTime: CMTime?
    let imagePicker = UIImagePickerController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        imagePickerController.delegate = self
    
        NotificationCenter.default.addObserver(self, selector: #selector(appHasEnteredBackground), name: Notification.Name.UIApplicationWillResignActive, object: nil)
    
        NotificationCenter.default.addObserver(self, selector: #selector(appWillEnterForeground), name: Notification.Name.UIApplicationWillEnterForeground, object: nil)
    }
    
    override func viewDidLayoutSubviews() {
        // I have a UIView Outlet for the AVPlayerLayer named viewForPlayerLayer.
    
        guard let playerLayer = playerLayer else { return }
        playerLayer.videoGravity = .resizeAspectFill
        viewForPlayerLayer.layer.insertSublayer(playerLayer, at: 0)
        playerLayer.frame = viewForPlayerLayer.bounds
        playerLayer.masksToBounds = true
    
    }
    
    @IBAction fileprivate func showLibraryButtonTapped(_ sender: UIButton) {
    
        imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary // .savedPhotosAlbum also tried 
        imagePicker.mediaTypes = [kUTTypeMovie as String]
        imagePicker.videoMaximumDuration = 60
        present(imagePicker, animated: true, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    
        if let url = info[UIImagePickerControllerMediaURL] as? URL{
    
              let asset = AVAsset(url: url)
              playerItem = AVPlayerItem(asset: asset)
              player = AVPlayer(playerItem: playerItem!)
              playerLayer = AVPlayerLayer(player: player)
              player?.play()
        }
        imagePicker.dismiss(animated: true, completion: nil)
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        imagePicker.dismiss(animated: true, completion: nil)
    }
    
    @objc fileprivate func appHasEnteredBackground() {
    
        currentPlayTime = player.currentTime()
    
        if player.isPlaying{
            player.pause()
        }
    
        playerLayer = nil
    }
    
    @objc fileprivate func appWillEnterForeground(){
    
        if let player = player, let currentPlayTime = currentPlayTime{
    
            playerLayer = AVPlayerLayer(player: player)
    
            player.seek(to: currentPlayTime, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
        }
    }
    
    }
    

1 个答案:

答案 0 :(得分:1)

如果您要求显示视频,则无法阻止出现在选择器中的循环/反弹实时照片视频,因为它们视频。但是,如果用户在委托方法中选择,则可以检测到一个。为此,UIImagePickerControllerMediaType的细粒度不够。相反,获取照片库授权并获取UIImagePickerControllerPHAsset密钥返回的PHAsset,并检查其playbackStyle。这将得出你想要的区别。

https://developer.apple.com/documentation/photos/phasset.playbackstyle

.videoLooping是一张反弹或缩放实时照片。