After the user gave us permission to access his Camera Roll. We would like to grab the data and upload it to our services from inside our app. Is there a way to access the video data from the file? The only way to open the video file is to create an AVAsset. But that's not enough for me.
I'm aware off
func requestExportSessionForVideo(_ asset: PHAsset!,
options options: PHVideoRequestOptions!,
exportPreset exportPreset: String!,
resultHandler resultHandler: ((AVAssetExportSession!,
[NSObject : AnyObject]!) -> Void)!) -> PHImageRequestID
But in my case I just want to upload the video to our service I don't want to do:
- first copy the video by doing an export into my local app data
- and then send that data up to our service.
- delete the data.
This approach above uses a lot of extra space and time and users with full 16GB iPhones it doesn't work well.
Ok this is what I tried so far using the 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
This logs the following:
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"}
Thanks to Paul suggestion I figured it out:
You have to create an PHImageManager requestAVAssetForVideo session in that block you have access to the file and read its data from the 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.")
}
}
}
来源:https://stackoverflow.com/questions/28398032/ios-photo-library-read-access