0

So I have an app that lets a user record a video or pick a video from their photo library, then upload the video to the server. After recording the video, I get the asset's URL.

I try to set the NSData to the assetURL with the following code:

NSString *vidString =[NSString stringWithFormat:@"%@",savedVideoURL];
NSURL *vidURL = [NSURL URLWithString:vidString];
NSData *videoData = [NSData dataWithContentsOfURL:vidURL];

savedVideoURL is the asset URL gained during recording. The asset URL looks something like this: assets-library://asset/asset.MOV?id=6EB7A04D-3DF8-44E5-A6D8-FD98461AE75E&ext=MOV

When I try to set the NSData to that url, NSData is still equal to nil afterwards.

I could not get the following solution to work: Get video NSData from ALAsset url iOS

Does anyone else know a method of setting the NSData to that asset's URL?

Thanks in advance!

Community
  • 1
  • 1
Will
  • 1,647
  • 17
  • 34

1 Answers1

8

Don't forget to Import : AssetsLibrary/ALAssetRepresentation.h

and code:

 ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
    [assetLibrary assetForURL:YOURURL resultBlock:^(ALAsset *asset) // substitute YOURURL with your url of video
    {
        ALAssetRepresentation *rep = [asset defaultRepresentation];
        Byte *buffer = (Byte*)malloc(rep.size);
        NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
        NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];//this is NSData may be what you want
        [data writeToFile:photoFile atomically:YES]; //you can remove this if only nsdata needed
    } 
    failureBlock:^(NSError *err) {
        NSLog(@"Error: %@",[err localizedDescription]);
    }];
Laz
  • 431
  • 7
  • 12