• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

iOS照片库读取访问

用户头像
it1352
帮助1

问题说明

在用户授予我们访问他的相机胶卷"的权限后.我们想获取数据并将其从应用程序内部上传到我们的服务中. 有没有办法从文件访问视频数据?打开视频文件的唯一方法是创建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:

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

上述方法会占用大量额外空间和时间,并且用户使用完整的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"}

正确答案

#1

多亏了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
系列文章
更多 icon
同类精品
更多 icon
继续加载