iOS照片库阅读权限

在用户允许我们访问他的相机胶卷之后。 我们想抓取数据,并从我们的应用程序内部上传到我们的服务。 有没有办法从文件中访问video数据? 打开video文件的唯一方法是创build一个AVAsset。 但这对我来说还不够

我知道了

func requestExportSessionForVideo(_ asset: PHAsset!, options options: PHVideoRequestOptions!, exportPreset exportPreset: String!, resultHandler resultHandler: ((AVAssetExportSession!, [NSObject : AnyObject]!) -> Void)!) -> PHImageRequestID 

但在我的情况,我只是想上传video到我不想做的服务:

  1. 首先通过导出到我的本地应用程序数据复制video
  2. 然后将这些数据发送到我们的服务。
  3. 删除数据。

上面的这个方法使用了很多额外的空间和时间,并且用户使用全部16GB的iPhone,它不能很好地工作。

好的,这是我迄今为止使用的URL尝试

 var anAcces = sourceURL?.startAccessingSecurityScopedResource if !NSFileManager.defaultManager().fileExistsAtPath(sourceURL!.path!) { NSLog("not exist") } var aFileCoordinator = NSFileCoordinator(filePresenter:nil) var anError: NSError? aFileCoordinator.coordinateReadingItemAtURL(sourceURL!, options:.ForUploading, error:&anError, byAccessor: { (newURL: NSURL!) -> Void in var data = NSData(contentsOfURL: newURL) }) if let unError = anError { NSLog("Error \(unError)") } sourceURL?.stopAccessingSecurityScopedResource 

这logging了以下内容:

 2015-02-08 16:20:01.947 Cameo[15706:2288691] not exist 2015-02-08 16:20:01.991 Cameo[15706:2288691] Error Error Domain=NSCocoaErrorDomain Code=257 "The operation couldn't be completed. (Cocoa error 257.)" UserInfo=0x170876480 {NSURL=file:///var/mobile/Media/DCIM/100APPLE/IMG_0155.MOV, NSFilePath=/var/mobile/Media/DCIM/100APPLE/IMG_0155.MOV, NSUnderlyingError=0x17005a9a0 "The operation couldn't be completed. Operation not permitted"} 

感谢Paul的build议,我明白了这一点:

您必须在该块中创buildPHImageManager requestAVAssetForVideo会话,才能访问该文件并从该URL读取其数据。

  let imageManager = PHImageManager.defaultManager() let videoRequestOptions = PHVideoRequestOptions() videoRequestOptions.deliveryMode = .HighQualityFormat videoRequestOptions.version = .Current videoRequestOptions.networkAccessAllowed = true videoRequestOptions.progressHandler = { (progress: Double, error: NSError!, stop: UnsafeMutablePointer<ObjCBool>, [NSObject : AnyObject]!) -> Void in NSLog("Progress: %@", progress.description) } videoRequestOptions.progressHandler = { (progress: Double, error: NSError!, stop: UnsafeMutablePointer<ObjCBool>, [NSObject : AnyObject]!) -> Void in NSLog("Progress: %@", progress.description) } imageManager.requestAVAssetForVideo(nextAsset, options: videoRequestOptions, resultHandler: { (avAsset: AVAsset!, avAudioMix: AVAudioMix!, info: [NSObject : AnyObject]!) -> Void in if let nextURLAsset = avAsset as? AVURLAsset { let sourceURL = nextURLAsset.URL if NSFileManager.defaultManager().fileExistsAtPath(sourceURL.path!) { NSLog("exist file") } var data = NSData(contentsOfURL: sourceURL) if let aData = data { NSLog("length : <\(aData.length)") } else { NSLog("no data read.") } } }