为什么imagePickerController.mediaTypes = [kUTTypeMovie as String]包括实时照片作为Bounce或Loop Effect作为视频? [英] Why is imagePickerController.mediaTypes = [kUTTypeMovie as String] including Live Photos as Bounce or Loop Effect as Video?

查看:450
本文介绍了为什么imagePickerController.mediaTypes = [kUTTypeMovie as String]包括实时照片作为Bounce或Loop Effect作为视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UIImagePicker仅从库中选择视频,并且将媒体类型设置为:

I'm using the UIImagePicker to pick only videos from my library and I set my media type to:

imagePicker.mediaTypes = [kUTTypeMovie as String]

我注意到,即使我只希望观看视频,但在展示该库时,即使库中有带BounceLoop效果的实时照片,它也会显示出来.如果我拍摄同一张照片并将其效果切换为LiveLong Exposure,它将不再显示在图库中.如果我将其切换回BounceLoop,它将重新出现.

I noticed that when the library is presented, even though I expect only videos, if there is a live photo in the library with a Bounce or Loop effect, it shows up also. If I take that same photo and switch it's effect to Live or Long Exposure it will no longer show up in the library. If I switch it back to Bounce or Loop it reappears.

我已经尝试了.photoLibrary.savedPhotosAlbum,并且发生了相同的事情.

I've tried both the .photoLibrary and the .savedPhotosAlbum and the same thing happens.

我问的原因是因为我想把它们排除在图书馆之外,而我只希望挑选xxx秒或以上的视频.

The reason I ask is because I want to keep them out of the library and I only want videos of xxx secs or more to be picked.

  1. 即使我仅选择视频,为什么这些效果类型仍会显示在图像选择器库中?
  2. 为什么只出现LoopBounce但没有出现Long ExposureLive?
  3. 我如何将它们拒之门外?
  1. Why do these effect types show up in the image picker library even though I'm picking video only?
  2. Why does only Loop and Bounce appear but Long Exposure and Live doesn't?
  3. How do I keep them out?

也许这将是一个单独的问题,但是由于它与相同的LiveBounce照片问题有关,因此我添加了它.我注意到,如果我将源自LiveBounce照片的网址放在AVPlayer中,如果我回到后台并返回,则当应用程序回到前台时会出现短暂的冻结,并且仅在这2种效果下发生(该效果不可用).当应用程序进入后台时,我可以使用删除AVPlayerLayer 的方法正确删除AVPlayerLayer,但是它仍然处于冻结状态(应用程序无响应),然后在播放器中初始化来自这些效果类型的网址.

Maybe this would be a separate question but since it pertains to the same Live and Bounce photo issue I added it. I noticed that if I put a url that was derived from a Live or Bounce photo inside an AVPlayer, if I go to the background and come back there is a brief freeze when the app is back in the foreground, and it only happens with those 2 effects (which shouldn't be available). When the app goes to the background I remove the AVPlayerLayer correctly using this method Remove AVPlayerLayer but it's still briefly frozen (app is unresponsive) in the foreground when a url from those to effect types are initialized inside the player.

代码:

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)
    }
}

}

推荐答案

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

You cannot prevent loop/bounce live photo videos appearing in the picker if you have asked to show videos, because they are videos. However, you can detect one if the user chooses one, in the delegate method. For this purpose, the UIImagePickerControllerMediaType is insufficiently fine-grained. Instead, get photo library authorization and get the PHAsset returned by the UIImagePickerControllerPHAsset key, and examine its playbackStyle. This will draw the distinctions you want.

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

.videoLooping是退回或缩放的实时照片.

.videoLooping is a bounce or zoom live photo.

这篇关于为什么imagePickerController.mediaTypes = [kUTTypeMovie as String]包括实时照片作为Bounce或Loop Effect作为视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆