如何将ALAssets转换为NSData?

时间:2014-09-26 10:24:56

标签: ios objective-c alassetslibrary

我需要将文件从ALAssetsLibrary转换为NSData,但我收到的是

我使用此代码

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

怎么能正确地做到这一点?

1 个答案:

答案 0 :(得分:3)

希望这会对你有所帮助

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