如何上传PHAsset(仅限视频和图像)?

时间:2016-07-14 18:40:52

标签: ios objective-c phasset

我想上传PHAsset,但我没有资产的文件路径。我可以使用下面的块方法上传文件。但这些是获取URL的异步方法。是否有任何同步方法可以这样做或以任何其他方式上传PHAsset文件。

图片资产网址:

[asset requestContentEditingInputWithOptions:editOptions
                               completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
        NSURL *imageURL = contentEditingInput.fullSizeImageURL;
    }];

视频文件网址:

[[PHImageManager defaultManager]requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {

                    NSURL *url = (NSURL *)[[(AVURLAsset *)asset URL] fileReferenceURL];




                }]; 

1 个答案:

答案 0 :(得分:1)

我从PHAsset获取视频并将其存储到本地文件路径以将其上传到服务器:以下是代码:

let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate",ascending:false)]
assets = PHAsset.fetchAssets(with:fetchOptions)

let resourceManager = PHAssetResourceManager.default()
let resource = PHAssetResource.assetResources(for: asset).first!
let name = resource.originalFilename
let videoLocalPath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(name)

//Storing the resource to local temporary path    
resourceManager.writeData(for: resource, toFile: videoLocalPath, options: nil, completionHandler: {error in
        if error != nil{
//       on success do the upload using the local file path
        }
    })

希望有所帮助。