1

I need convert a file from ALAssetsLibrary to NSData, but I'm getting nil.

I use this code

 path = file:///assets-library://asset/asset.mp4?....
 NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
 NSData *dates = [NSData dataWithContentsOfURL: url];

How can do this correctly?

Ricardo Sanchez-Saez
  • 9,466
  • 8
  • 53
  • 92
user3745888
  • 6,143
  • 15
  • 48
  • 97

1 Answers1

3

Hope this will help u

 ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
            [assetLibrary assetForURL:[[self.imagedata objectAtIndex:i] valueForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) 
            {
                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 save image later
            } 
            failureBlock:^(NSError *err) {
                NSLog(@"Error: %@",[err localizedDescription]);
            }];
dheeru
  • 395
  • 3
  • 13
  • 1
    I have used this code to get GIF image, this works but not properly the animation state is not staying with the GIF image and its size get compressed,any other option will be useful. – Pankaj Bhardwaj Jul 04 '15 at 17:49