2012-05-10 27 views
0

因此,我有一个应用程序可让用户录制视频或从其照片库中选择视频,然后将视频上传到服务器。录制视频后,我会看到资产的网址。iOS 5 - ALAssets库 - 将资源中的视频URL转换为NSDATA文件

我尝试NSData的用下面的代码设置为assetURL:

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

savedVideoURL是在录制过程中取得的资产的URL。资产的URL看起来像这样:assets-library://asset/asset.MOV?id=6EB7A04D-3DF8-44E5-A6D8-FD98461AE75E&ext=MOV

当我尝试将NSData设置为该网址时,NSData之后仍然等于零。

我不能让下面的解决方案正常工作:Get video NSData from ALAsset url iOS

有谁人知道NSData的设置,以该资产的URL的方法?

在此先感谢!

回答

8

不要忘记导入:AssetsLibrary/ALAssetRepresentation.h

和代码:

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