升级到Xcode 7后,UIImagePickerControllerMediaURL上没有文件

时间:2015-10-12 05:34:03

标签: ios xcode uiimagepickercontroller swift2

将我的Swift视频应用升级到Swift 2.0 / Xcode 7 / iOS 9后,除了从UIImagePickerController获取视频外,一切正常。所有权限都会检查相机和库。我可以同时访问并录制新视频或从库中选择现有视频。但是,当调用委托方法imagePickerController:didFinishPickingMediaWithInfo时,NSFileManager会告诉我UIImagePickerControllerMediaURL处没有文件。在dismissViewControllerAnimated上调用UIImagePickerController之前和之后都是如此。

之前它完美无缺,并且没有重大的代码更改来适应升级。我发现没有其他类似的问题在网上报道,所以我认为我必须搞砸像文件夹权限,但我无法弄清楚是什么。我想也许使用NSAppTransportSecurityfile:///网址有关,但是问题仍然存在于iOS 7,8和9中,无论我的信息中是否存在该词典的.plist。

如果有人能给我一些线索,我会非常感激。我在iPhone 6,5s和& 4s具有相同的结果。我确实记得在Swift 2升级过程中使用“kUTTypeMovie”进行回合。下面的“[kUTTypeMovie as String]”是我发现Xcode唯一接受的东西。这可能是一个弱点,但我怀疑它,因为它似乎按预期工作。我提到它的唯一原因是,如果您不要求类型,该应用程序无权接收它。

其他信息

我可以使用UIImagePickerController正常从相机/库中获取图像。一时兴起,我想也许我可以通过抓住原始视频并自己压缩来解决这个问题,但是NSFileManager告诉我UIImagePickerControllerReferenceURL上也没有文件。除非我的应用程序以某种方式被拒绝访问,否则我不知道这是怎么可能的。

  • 使用以下代码记录:在以下位置没有文件: ?资产库://asset/asset.MOV ID = 22A4CFF1-BBC0-4084-990D-B217908F0616&安培; EXT = MOV
  • 这太奇怪了,我已经向Apple提交了一份错误报告。

此处的相关代码:

 @IBAction func actionTakeVideo(sender: AnyObject) {
    if videoPickerController == nil {
        videoPickerController = UIImagePickerController()
        videoPickerController.delegate = self
        videoPickerController.mediaTypes = [kUTTypeMovie as String]
        videoPickerController.videoMaximumDuration = NSTimeInterval(maxVideoDuration-1)
        videoPickerController.allowsEditing = true
        videoPickerController.videoQuality = UIImagePickerControllerQualityType.TypeHigh
 }

...

// From Library
       let status:ALAuthorizationStatus = ALAssetsLibrary.authorizationStatus()
       if status != ALAuthorizationStatus.Authorized{
           print("Library not authorized: status != ALAuthorizationStatus.Authorized")
       } else {
           self.videoPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
           self.presentViewController(self.videoPickerController, animated: true, completion: nil)
       }

...

//From Camera
      if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
            self.videoPickerController.sourceType = UIImagePickerControllerSourceType.Camera
            self.videoPickerController.cameraDevice = UIImagePickerControllerCameraDevice.Front
            self.presentViewController(self.videoPickerController, animated: true, completion: nil)
      } 

...

//Delegate
     func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
         let manager = NSFileManager.defaultManager();
         let videoURL = info[UIImagePickerControllerMediaURL] as! NSURL
         if manager.fileExistsAtPath(videoURL.absoluteString) {
             print("THERE IS a file at: \(videoURL.absoluteString)")
         } else {
             print("THERE IS NOT a file at: \(videoURL.absoluteString)") // ALWAYS LANDS HERE.
         }
     }

UIImagePickerControllerMediaURL就像,file:///private/var/mobile/Containers/Data/Application/6754830D-ER98-31OL-GU34-78067GD908PI/tmp/trim.578E437C-EEBC-4BC5-AD5C-C351BD0822B2.MOV < / p>

1 个答案:

答案 0 :(得分:0)

我知道这个问题已经很老了,但我今天偶然发现了同样的错误。

问题是您从videoURL.absoluteString获取文件URL字符串并将其传递给NSFileManager的fileExistsAtPath方法而不是路径。您应该通过阅读NSURL&#34;路径&#34;来获取文件的路径。属性。

从videoURL.absoluteString读取的文件URL如下所示:

  

文件:///private/var/mobile/Containers/Data/Application/6754830D-ER98-31OL-GU34-78067GD908PI/tmp/trim.578E437C-EEBC-4BC5-AD5C-C351BD0822B2.MOV

从videoURL.path读取的文件路径如下所示:

  

/private/var/mobile/Containers/Data/Application/6754830D-ER98-31OL-GU34-78067GD908PI/tmp/trim.578E437C-EEBC-4BC5-AD5C-C351BD0822B2.MOV

以下代码:

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
         let manager = NSFileManager.defaultManager();
         let videoURL = info[UIImagePickerControllerMediaURL] as! NSURL
         if manager.fileExistsAtPath(videoURL.absoluteString) {
             print("THERE IS a file at: \(videoURL.absoluteString)")
         } else {
             print("THERE IS NOT a file at: \(videoURL.absoluteString)") // ALWAYS LANDS HERE.
         }
     }

必须更改为:

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
         let manager = NSFileManager.defaultManager();
         let videoURL = info[UIImagePickerControllerMediaURL] as! NSURL
         if manager.fileExistsAtPath(videoURL.path) {
             print("THERE IS a file at: \(videoURL.path)")
         } else {
             print("THERE IS NOT a file at: \(videoURL.path)") // ALWAYS LANDS HERE.
         }
     }

请记住,UIImagePickerControllerMediaURL可能不存在:

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
         if let videoURL = info[UIImagePickerControllerMediaURL] as? URL {
             if FileManager.default.fileExistsAtPath(videoURL.path) {
                 print("THERE IS a file at: \(videoURL.path)")
             } else {
                 print("THERE IS NOT a file at: \(videoURL.path)") // ALWAYS LANDS HERE.
             }
         }
     }