【发布时间】:2014-08-21 16:36:25
【问题描述】:
我正在尝试将相册中的视频保存到我的文档目录中。它适用于少于 1 分钟的视频。但是当我尝试保存超过 1 分钟的视频时,我的应用程序崩溃了。这仅在 iPhone 中发生,在 iPad 中也适用于较大的视频。
这是我的代码:
else if([mediaType isEqualToString:ALAssetTypeVideo])
{
ALAssetsLibrary *librarys = [[ALAssetsLibrary alloc] init];
[librarys enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group setAssetsFilter:[ALAssetsFilter allVideos]];
if ([group numberOfAssets] > 0)
{
for (int j = 0; j < [group numberOfAssets]; j++)
{
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:j]
options:0
usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop)
{
if (alAsset)
{
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
NSURL *url = [representation url];
if ([[dict objectForKey:@"UIImagePickerControllerReferenceURL"] isEqual:url])
{
Byte *buffer = (Byte*)malloc((unsigned)[representation size]);
//NSUInteger buffered = [representation getBytes:buffer fromOffset:0.0 length:representation.size error:nil];
//Byte *buffer = ((Byte*)representation.size);
//NSUInteger chunkSize = 100 * 1024;
// uint8_t *buffer = malloc(chunkSize * sizeof(uint64_t));
NSUInteger buffered = [representation getBytes:buffer fromOffset:0.0 length:(NSUInteger)representation.size error:nil];
NSData *videoCameraData = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
NSString *savedImagePath = [docDirectory stringByAppendingPathComponent:str_Header];
NSError *error;
[[NSFileManager defaultManager] createDirectoryAtPath:savedImagePath withIntermediateDirectories:NO attributes:nil error:&error];
我的应用程序崩溃了:
NSUInteger buffered = [representation getBytes:buffer fromOffset:0.0 length:(NSUInteger)representation.size error:nil];
错误:
malloc: *** mach_vm_map(size=310386688) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
【问题讨论】:
-
从你的帖子中我会说你要求的内存太多......
-
@Volker 有什么锻炼方法可以保存这些视频而又不消耗太多内存吗?
-
分割内存使用...或使用交换文件...环形缓冲区...任何可以减少内存分配大小的东西。
-
嗨@volker...这个 riven 的回答解决了我的问题。 stackoverflow.com/questions/8791049/…
标签: objective-c alassetslibrary