我需要将文件从ALAssetsLibrary
转换为NSData
,但是我得到的是nil。
我用这个代码
path = file:///assets-library://asset/asset.mp4?....
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
NSData *dates = [NSData dataWithContentsOfURL: url];
如何正确执行此操作?
最佳答案
希望这对你有帮助
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]);
}];
关于ios - 怎么把ALAssets转换成NSData?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26057394/