Проблема с памятью при чтении видеокадров iPhone

У меня проблемы с памятью при чтении видеокадров из существующего видео, выбранного из библиотеки iPhone. Сначала я добавил сами кадры UIImage в массив, но через некоторое время подумал, что массив слишком велик для памяти, поэтому вместо этого я сохраняю UIImages в папке с документами и добавляю путь к изображению в массив. Тем не менее, я все еще получаю те же предупреждения о памяти, даже если проверяю выделение с помощью инструментов. Общий объем выделенной памяти никогда не превышает 2,5 МБ. Тоже утечек не обнаружено ... Может кто что придумает?

-(void)addFrame:(UIImage *)image
{
    NSString *imgPath = [NSString stringWithFormat:@"%@/Analysis%d-%d.png", docFolder, currentIndex, framesArray.count];       
    [UIImagePNGRepresentation(image) writeToFile:imgPath atomically:YES];
    [framesArray addObject:imgPath];    
    frameCount++;      
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissModalViewControllerAnimated:YES];
    [framesArray removeAllObjects];    
    frameCount = 0;          

    // incoming video
    NSURL *videoURL = [info valueForKey:UIImagePickerControllerMediaURL];
    //NSLog(@"Video : %@", videoURL);

    // AVURLAsset to read input movie (i.e. mov recorded to local storage)
    NSDictionary *inputOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
    AVURLAsset *inputAsset = [[AVURLAsset alloc] initWithURL:videoURL options:inputOptions];     

    // Load the input asset tracks information
    [inputAsset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler: ^{        

        NSError *error = nil;
        nrFrames = CMTimeGetSeconds([inputAsset duration]) * 30;
        NSLog(@"Total frames = %d", nrFrames);

        // Check status of "tracks", make sure they were loaded    
        AVKeyValueStatus tracksStatus = [inputAsset statusOfValueForKey:@"tracks" error:&error];
        if (!tracksStatus == AVKeyValueStatusLoaded)
            // failed to load
            return;        

        /* Read video samples from input asset video track */
        AVAssetReader *reader = [AVAssetReader assetReaderWithAsset:inputAsset error:&error];

        NSMutableDictionary *outputSettings = [NSMutableDictionary dictionary];
        [outputSettings setObject: [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]  forKey: (NSString*)kCVPixelBufferPixelFormatTypeKey];
        AVAssetReaderTrackOutput *readerVideoTrackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[[inputAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] outputSettings:outputSettings];


        // Assign the tracks to the reader and start to read
        [reader addOutput:readerVideoTrackOutput];
        if ([reader startReading] == NO) {
            // Handle error
            NSLog(@"Error reading");
        }

        NSAutoreleasePool *pool = [NSAutoreleasePool new];
        while (reader.status == AVAssetReaderStatusReading)
        {            
            if(!memoryProblem)
            {
                CMSampleBufferRef sampleBufferRef = [readerVideoTrackOutput copyNextSampleBuffer];
                if (sampleBufferRef) 
                {
                    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBufferRef);
                    /*Lock the image buffer*/
                    CVPixelBufferLockBaseAddress(imageBuffer,0); 
                    /*Get information about the image*/
                    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer); 
                    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
                    size_t width = CVPixelBufferGetWidth(imageBuffer); 
                    size_t height = CVPixelBufferGetHeight(imageBuffer); 

                    /*We unlock the  image buffer*/
                    CVPixelBufferUnlockBaseAddress(imageBuffer,0);

                    /*Create a CGImageRef from the CVImageBufferRef*/
                    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
                    CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 
                    CGImageRef newImage = CGBitmapContextCreateImage(newContext); 

                    /*We release some components*/
                    CGContextRelease(newContext); 
                    CGColorSpaceRelease(colorSpace);

                    UIImage *image= [UIImage imageWithCGImage:newImage scale:[UIScreen mainScreen].scale orientation:UIImageOrientationRight];          
                    //[self addFrame:image];
                    [self performSelectorOnMainThread:@selector(addFrame:) withObject:image waitUntilDone:YES];

                    /*We release the CGImageRef*/
                    CGImageRelease(newImage);                    

                    CMSampleBufferInvalidate(sampleBufferRef);
                    CFRelease(sampleBufferRef);
                    sampleBufferRef = NULL;
                }
            }
            else 
            {                
                break;
            }            
        }
        [pool release];

        NSLog(@"Finished");        
    }];   
}

person Bob de Graaf    schedule 27.03.2012    source источник


Ответы (1)


Вы делаете одно и пытаетесь.

Переместите NSAutoreleasePool внутрь while петли и слейте воду внутри петли.

Чтобы было так:

while (reader.status == AVAssetReaderStatusReading)
{            
    NSAutoreleasePool *pool = [NSAutoreleasePool new];

    .....

    [pool drain];
} 
person Ilanchezhian    schedule 27.03.2012
comment
ты гений, работает !!! Мне все же интересно, как вы думаете об этом? Почему это должно работать при помещении autoreleasepool в цикл while? - person Bob de Graaf; 27.03.2012
comment
Если он находится вне цикла, тогда пул будет опорожнен только в конце цикла. Но до того, как цикл закончится, память накапливается и вылетает. Так что, если он находится внутри, на каждой итерации автоматически выпущенные объекты будут освобождаться. - person Ilanchezhian; 27.03.2012