Reckoner
Reckoner

Reputation: 1041

Not able to upload Photos gallery video but able upload videos shoot from camera iOS

I am working on an app where I need to upload videos to server.Now here I have 2 things:

  1. Shoot video using UIImagePickerController,generate a thumbnail and then upload to server
  2. Pick video from Photos gallery, generate thumbnail and then upload to server.

Now the only difference between the two is:

When I use 'generateImageAsynchronouslyForTimes:completionHandler:' method,I get a call in its completionHandler block and I get an AVAsset.Now I am using below code to get its URL:

    NSURL *path_url = [(AVURLAsset*)asset URL];

This is where I think things are getting messed up because I am getting something like this in case 2(when I pick video from gallery):

file:///var/mobile/Media/DCIM/102APPLE/IMG_2439.mp4

So I can't upload it while case 1 is is working fine.Is it something related to sandbox?

What's the difference between these 2 paths?

  1. file:///private/var/mobile/Containers/Data/Application/DA4632E3-FA25-4EBE-9102-62495BF105BF/tmp/trim.07786CFE-2477-4146-9EA0-0A04042A8D05.MOV"
  2. file:///var/mobile/Media/DCIM/102APPLE/IMG_2439.mp4

I guess its appSandbox path in 1)

Upvotes: 0

Views: 197

Answers (1)

Reckoner
Reckoner

Reputation: 1041

In iOS, every app is like an island and there is a sandbox environment for it.So if you like to upload your video that is not in your sandbox,you will have to copy that video to your sandbox and then you can upload it.This is how you can do this:

NSURL *path_url = [(AVURLAsset*)asset URL];

PHAssetResource *asset_resource = [[PHAssetResource assetResourcesForAsset:[fetchResult lastObject]]firstObject];

PHAssetResourceRequestOptions *options = [PHAssetResourceRequestOptions new];
options.networkAccessAllowed = YES;
NSURL *newURL = [self getSandboxURLFromURL:path_url];

[[PHAssetResourceManager defaultManager] writeDataForAssetResource:asset_resource toFile:newURL options:options completionHandler:^(NSError * _Nullable error) {

//here you will get the newURL that you will use...
}];

//method to get sandbox URL

-(NSURL*)getSandboxURLFromURL:(NSURL*)photos_gallery_url{

    NSString *last_path_component = [photos_gallery_url lastPathComponent];
    NSString *pathToWrite = [NSTemporaryDirectory() stringByAppendingString:last_path_component];
    NSURL *localpath = [NSURL fileURLWithPath:pathToWrite];
    return localpath;
}

Upvotes: 1

Related Questions