KDRocks
KDRocks

Reputation: 161

How to get video url from ALAsset Url for uploading a video on server?

I am getting assets-library://asset/asset.mp4?id=F0D95698-C982-4723-8959-502CE595E3D1&ext=mp4 url when a video is selected by using ELCImagePickerController . Now, I've to retrieve video name and media url for uploading this video on server using asiDataFormRequest.

Video uploading is working fine when i pick a video by using ImagePickerViewController.Right now i have to select multiple video so i am using ELCImagePickerController. But it give video url as given bellow. assets-library://asset/asset.mp4?id=F0D95698-C982-4723-8959-502CE595E3D1&ext=mp4

How I can Convert this url in MEdia Url type format .My main aim to upload this video using asihttpdatafromrequest and get this size,name.

Upvotes: 1

Views: 2124

Answers (1)

Vaibhav Sharma
Vaibhav Sharma

Reputation: 1123

@KDRocks. this Code is Running successfully for get the Name with complete Path.

-(NSString*) videoAssetURLToTempFile:(NSURL*)url
{
    NSString * surl = [url absoluteString];
    NSString * ext = [surl substringFromIndex:[surl rangeOfString:@"ext="].location + 4];
    NSTimeInterval ti = [[NSDate date]timeIntervalSinceReferenceDate];
    NSString * filename = [NSString stringWithFormat: @"%f.%@",ti,ext];
    NSString * tmpfile = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation * rep = [myasset defaultRepresentation];
        NSUInteger size = [rep size];
        const int bufferSize = 8192;
        NSLog(@"Writing to %@",tmpfile);
        FILE* f = fopen([tmpfile cStringUsingEncoding:1], "wb+");
        if (f == NULL) 
        {
            NSLog(@"Can not create tmp file.");
            return;
        }
        Byte * buffer = (Byte*)malloc(bufferSize);
        int read = 0, offset = 0, written = 0;
        NSError* err;
        if (size != 0) {
        do {
            read = [rep getBytes:buffer
                      fromOffset:offset
                          length:bufferSize
                           error:&err];
            written = fwrite(buffer, sizeof(char), read, f);
            offset += read;
        } while (read != 0);
        }
        fclose(f);
    };
    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"Can not get asset - %@",[myerror localizedDescription]);

    };
    if(url)
    {
        ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
        [assetslibrary assetForURL:url
                   resultBlock:resultblock
                  failureBlock:failureblock];
    }
    return tmpfile;
}

Upvotes: 1

Related Questions