2014-09-26 43 views
1

我需要将文件从ALAssetsLibrary转换为NSData,但我得到零。如何转换ALAssets至NSData?

我使用此代码

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

如何正确地做到这一点?

+0

[使用ALAssetsLibrary和ALAsset采取了图片作为NSData的]的可能重复(http://stackoverflow.com/questions/8473110/using-alassetslibrary-and-alasset-拿出图像为nsdata) – 2014-09-26 11:03:28

+0

你有答案或没有? – 2014-10-14 09:51:57

回答

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

我已经使用这段代码来获取GIF图片,但是这个工作正常,但是不正确,动画状态并没有停留在GIF图片上,并且它的大小被压缩了,其他任何选项都会有用。 – 2015-07-04 17:49:53