gpt4 book ai didi

ios - 如何将实时照片上传到服务器?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:04:07 27 4
gpt4 key购买 nike

如何将实时照片上传到服务器?我正在使用 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。我们既不能像上传照片或视频那样上传它,也不能像处理慢动作视频那样从 2 个或更多文件的组合创建新文件,因为我们必须将实时照片的两个文件都发送到服务器。

很明显,我们必须将这两个文件发送到服务器。但是如何在单个请求中发送多个文件。

最佳答案

阅读这个问题的一个非常有趣的解决方案。这表明我们不需要将 *jpg 和 *.mov 都上传到服务器以获取实时照片。我们只能从视频中点上传视频并在服务器上创建图像。

The Live Photo begins with the middle of the video - I just read this video, find the middle, save the image.


We can also create a Live Photo from a video as the iPhone 6s does. The 6s captures a short video with the photo and plays the video when the photo is 3D touched. However, by using this method of creating a PHLivePhoto and playing it using the PHLivePhotoView, we can get it to work on any iPhone, such as my regular iPhone 6.

来源:See Here.

我们可以得到一张实况照片的视频URL。参见 How to get extract video URL from a live photo.

但我不确定这是否是解决此问题的好方法。谁能告诉我这是否是一个好的解决方案。

关于ios - 如何将实时照片上传到服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39370037/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com