Проблема с памятью при чтении видеокадров iPhone - PullRequest
2 голосов
/ 27 марта 2012

У меня проблемы с памятью при чтении видеокадров из существующего видео, выбранного из библиотеки iPhone.Сначала я добавил сами UIImage-кадры в массив, но подумал, что через некоторое время массив стал слишком большим для памяти, поэтому вместо этого я сохраняю UIImages в папке документов и добавляю imagepath в массив.Тем не менее, я по-прежнему получаю те же предупреждения о памяти, хотя проверяю с помощью инструментов для выделения.Общий объем выделенной памяти никогда не превышает 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");        
    }];   
}

1 Ответ

2 голосов
/ 27 марта 2012

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

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

Чтобы это выглядело следующим образом:

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

    .....

    [pool drain];
} 
...