How to upload PHAsset (video and image only)?

心不动则不痛 提交于 2021-01-27 19:05:05

问题


I want to upload PHAsset but I do not have the file path of the asset. I am able to upload the file using the below block methods. But these are asynchronous methods to get the URLs. Is there any synchronous method to do so or any other way to upload the PHAsset files.

Image Asset URL:

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

Video file URL:

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

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




                }]; 

回答1:


I am fetching video from PHAsset and storing that to local file path to upload it to server: here is the following code:

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

Hope that helps.



来源:https://stackoverflow.com/questions/38381851/how-to-upload-phasset-video-and-image-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!