[英]ALAssets get video data
I am trying to access video data from ALAssets library using the below code 我正在尝试使用以下代码从ALAssets库访问视频数据
ALAssetRepresentation *rep = [asset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rep.size);
NSError *error = nil;
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:&error];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
It works fine for small video as well as pictures, But if am trying to get a large video, the code crashes saying 它适用于小视频和图片,但如果我想要获得一个大视频,代码崩溃说
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSConcreteData initWithBytes:length:copy:freeWhenDone:bytesAreVM:]: absurd length: 4294967295, maximum size: 2147483648 bytes'
*由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'* - [NSConcreteData initWithBytes:length:copy:freeWhenDone:bytesAreVM:]:absurd length:4294967295,maximum size:2147483648 bytes'
I don't have an idea what's going on. 我不知道发生了什么。 Any one any thoughts?
任何一个想法?
Thanks in advance! 提前致谢!
I found the solution. 我找到了解决方案。 I guess the crash may be due to huge memory spike when we upload large files, because I am buffering data.
我想当我们上传大文件时崩溃可能是由于巨大的内存峰值,因为我正在缓冲数据。 Now I read file data as 5 MB chunks and this fix the crash.
现在我将文件数据读取为5 MB块,这可以解决崩溃问题。 I am pasting my code below.
我在下面粘贴我的代码。
- (NSData *)getDataPartAtOffset:(NSInteger)offset {
__block NSData *chunkData = nil;
if (fileAsset_){
static const NSUInteger BufferSize = PART_SIZE; // 5 MB chunk
ALAssetRepresentation *rep = [fileAsset_ defaultRepresentation];
uint8_t *buffer = calloc(BufferSize, sizeof(*buffer));
NSUInteger bytesRead = 0;
NSError *error = nil;
@try
{
bytesRead = [rep getBytes:buffer fromOffset:offset length:BufferSize error:&error];
chunkData = [NSData dataWithData:[NSData dataWithBytesNoCopy:buffer length:bytesRead freeWhenDone:NO]];
}
@catch (NSException *exception)
{
free(buffer);
chunkData = nil;
// Handle the exception here...
}
free(buffer);
} else {
NSLog(@"failed to retrive Asset");
}
return chunkData;
} }
And I I'll call this function as 而我将把这个功能称为
int offset = 0; // offset that keep tracks of chunk data
do {
@autoreleasepool {
NSData *chunkData = [self getDataPartAtOffset:offset];;
if (!chunkData || ![chunkData length]) { // finished reading data
break;
}
// do your stuff here
offset +=[chunkData length];
}
} while (1);
chilitechno 在这里为我工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.