8

I am trying to save video from album to my document directory. It's working fine for videos less than 1 minute. But when I am trying to save videos more than 1 minute, my app is getting crashed. This is happening only in iPhone, in iPad it's working working for larger videos as well.

This is my code :

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

My app is getting crashed at :

NSUInteger buffered = [representation getBytes:buffer fromOffset:0.0 length:(NSUInteger)representation.size error:nil];

Error :

 malloc: *** mach_vm_map(size=310386688) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Gill
  • 81
  • 1
  • 5
  • from your post I would say you request too much memory... – Volker Jul 01 '14 at 08:12
  • @Volker Is there any workout to save these videos without consuming so much memory? – Gill Jul 01 '14 at 08:13
  • splitting up the memory usage... or using a swap file... a ring buffer... anything that does reduce the allocation size of memory. – Volker Jul 02 '14 at 07:25
  • 2
    hi @volker...this answer of riven solved my issue. http://stackoverflow.com/questions/8791049/get-video-nsdata-from-alasset-url-ios – Gill Jul 03 '14 at 07:38

1 Answers1

0

I was getting this bug while using Core Audio. I was able to fix it by setting compiler optimization to "-O0 None."

Pescolly
  • 922
  • 11
  • 18