iOS照片库读取访问
问题说明
在用户授予我们访问他的相机胶卷"的权限后.我们想获取数据并将其从应用程序内部上传到我们的服务中. 有没有办法从文件访问视频数据?打开视频文件的唯一方法是创建AVAsset.但这对我来说还不够.
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.
我知道了
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:
- 首先通过导出到我的本地应用程序数据中来复制视频
- 然后将数据发送到我们的服务.
- 删除数据.
上述方法会占用大量额外空间和时间,并且用户使用完整的16GB iPhone不能正常工作.
This approach above uses a lot of extra space and time and users with full 16GB iPhones it doesn't work well.
好,这是我到目前为止使用URL尝试过的
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
这将记录以下内容:
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的建议,我弄清楚了:
Thanks to Paul suggestion I figured it out:
您必须在该块中创建PHImageManager requestAVAssetForVideo会话,您才能访问该文件并从url中读取其数据.
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.")
}
}
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfjbck
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20