如何将实时照片上传到服务器?我正在使用 AFNetworking 上传图像、视频和慢动作视频。图片和视频的上传非常简单。
上传图片和视频
//Physical location i.e. URL for video
PHVideoRequestOptions *options = [PHVideoRequestOptions new];
options.networkAccessAllowed = YES;
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
if ([asset isKindOfClass:[AVURLAsset class]]) {
NSLog(@"%@",((AVURLAsset *)asset).URL);
}
}];
//Physical location i.e. URL for an image
[asset requestContentEditingInputWithOptions:nil
completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
NSURL *imageURL = contentEditingInput.fullSizeImageURL;
}];
这些 URL 用于上传图像和视频文件。
// manager is of class AFHTTPSessionManager initialised with background session.
NSURLsession *uploadTask=[manager uploadTaskWithRequest:request
fromFile:[NSURL URLWithString:fdi.filePath]
progress:nil
completionHandler:nil];
上传慢动作文件
慢动作文件是 2 个或更多视频文件的组合。如果您尝试像普通视频文件一样上传它,那么它会失去其慢动作功能。要解决这个问题,首先我们必须创建一个慢动作文件,然后将其保存在磁盘上,然后上传。
PHVideoRequestOptions *options = [PHVideoRequestOptions new];
options.networkAccessAllowed = YES;
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
if(([asset isKindOfClass:[AVComposition class]] && ((AVComposition *)asset).tracks.count == 2)){
//Added by UD for slow motion videos. See Here: https://overflow.buffer.com/2016/02/29/slow-motion-video-ios/
//Output URL
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths.firstObject;
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"mergeSlowMoVideo-%d.mov",arc4random() % 1000]];
NSURL *url = [NSURL fileURLWithPath:myPathDocs];
//Begin slow mo video export
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL = url;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;
[exporter exportAsynchronouslyWithCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if (exporter.status == AVAssetExportSessionStatusCompleted) {
NSURL *URL = exporter.outputURL;
self.filePath=URL.absoluteString;
NSURLsession *uploadTask=[manager uploadTaskWithRequest:request
fromFile:[NSURL URLWithString:self.filePath]
progress:nil
completionHandler:nil];
//Use above method or use the below one.
// NSData *videoData = [NSData dataWithContentsOfURL:URL];
//
//// Upload
//[self uploadSelectedVideo:video data:videoData];
}
});
}];
}
}];
如何上传实况照片?
Live photo 是 2 个文件的组合,*.jpg 和 *mov。我们既不能像照片或视频那样上传它,也不能像使用慢动作视频那样从两个或多个文件的组合中创建一个新文件,因为我们必须将实时照片的两个文件都发送到服务器。
很明显,我们必须将这两个文件都发送到服务器。但是如何在一个请求中发送多个文件。