gpt4 book ai didi

iphone - ALAssets 获取视频数据

转载 作者:行者123 更新时间:2023-12-03 19:52:37 24 4
gpt4 key购买 nike

我正在尝试使用以下代码从 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];

它适用于小视频和图片,但如果我尝试获取大视频,代码就会崩溃

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSConcreteData initWithBytes:length:copy:freeWhenDone:bytesAreVM:]: absurd length: 4294967295, maximum size: 2147483648 bytes'

我不知道发生了什么。大家有什么想法吗?

提前致谢!

最佳答案

我找到了解决方案。我猜崩溃可能是由于我们上传大文件时出现的巨大内存峰值,因为我正在缓冲数据。现在我将文件数据读取为 5 MB block ,这解决了崩溃问题。我将我的代码粘贴在下面。

- (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;

}

我将这个函数称为

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

关于iphone - ALAssets 获取视频数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16197076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com