iOS照片库读取访问 [英] iOS Photo Library read access

查看:253
本文介绍了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:

  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"}

推荐答案

多亏了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.")
            }
        }
    }

这篇关于iOS照片库读取访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆