2

How do I upload Live photos to a server? I am using AFNetworking for the upload of images, videos, and Slow motion videos. The upload of images and videos is very straightforward.

Upload of Images and Videos

//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;

 }];

These URLs are used for the upload of images and video Files.

// manager is of class AFHTTPSessionManager initialised with background session.   

NSURLsession *uploadTask=[manager uploadTaskWithRequest:request
                                               fromFile:[NSURL URLWithString:fdi.filePath] 
                                               progress:nil
                                      completionHandler:nil];

Upload of Slow Motion file

Slow motion file is a combination of 2 or more video files. If you try to upload it like a normal video file then it would loose its slow motion feature. To work this out, first we have to create a slow motion file, then save it on the disk and then upload it.

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


            } 
        }];

How to upload a Live Photo?

A Live photo is combination of 2 files, *.jpg and *mov. We can neither upload it like a photo or video nor we can create a new file from the combination of 2 or more files like we did with the slow motion video because we have to send both the files of a live photo to the server.

It is clear that we have to send both the files to the server. But how to send multiple files in a single request.

Utsav Dusad
  • 2,139
  • 4
  • 31
  • 52

1 Answers1

1

Read a very interesting solution to this problem. It suggests that we do not need to upload both the *jpg and *.mov to the server for live photo. We can only upload the video and create image at the server from the mid point of video.

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.

Source: See Here.

We can get the video URL of a live photo. See How to get extract video URL from a live photo.

But I am not sure whether this would be good solution for this problem. Can any tell me whether it is a good solution or not.

Community
  • 1
  • 1
Utsav Dusad
  • 2,139
  • 4
  • 31
  • 52