user3745888
user3745888

Reputation: 6353

How to convert from ALAssets to NSData?

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?

Upvotes: 1

Views: 3397

Answers (1)

dheeru
dheeru

Reputation: 395

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

Upvotes: 3

Related Questions