问题
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