iPhone - 收到内存警告

时间:2012-07-25 06:27:32

标签: iphone objective-c memory-management avfoundation

我正在使用aVFoundation框架从视频资产中读取CMSampleBufferRef,在while循环中我得到的图像如下所示:

-(void)splitImagesFromVideo
{
    if (images != nil) {
        [images release];
        images = nil;
    }
    images =[[NSMutableArray alloc] init];


    NSURL *fileURL = [[NSBundle mainBundle]
                      URLForResource:@"3idiots" withExtension:@"mov"];

    NSLog(@"video: %@",videoURL);

    AVAsset *theAVAsset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];

    NSError *error = nil;
    float width = theAVAsset.naturalSize.width;
    float height = theAVAsset.naturalSize.height; 
    AVAssetReader *mAssetReader = [[AVAssetReader alloc] initWithAsset:theAVAsset error:&error]; 



    NSArray *videoTracks = [theAVAsset tracksWithMediaType:AVMediaTypeVideo]; 
    AVAssetTrack *videoTrack = [videoTracks objectAtIndex:0];


    NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
    AVAssetReaderTrackOutput* mAssetReaderOutput = [[AVAssetReaderTrackOutput alloc] initWithTrack:videoTrack outputSettings:options];

    [mAssetReader addOutput:mAssetReaderOutput];



    BOOL success = [mAssetReader startReading];
    NSInteger val = 1;

    while ( [mAssetReader status]==AVAssetReaderStatusReading ){
        CMSampleBufferRef buffer = [mAssetReaderOutput copyNextSampleBuffer];//read next image.
        if (buffer) {

            UIImage *img = [self imageFromSampleBuffer:buffer];
            if (img != nil) {

                NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
                img = [self imageOfSize:CGSizeMake(320, 480) fromImage:img];
                [images addObject:img];
                [pool release];
            }

            CMSampleBufferInvalidate(buffer);
            CFRelease(buffer);
            buffer = nil;

        }
        NSLog(@"value: %d", val++);
    }

    [images removeLastObject];
    NSLog(@"img count: %d", [images count]);
    NSLog(@"imgs: %@",images);

    [theAVAsset release];
    [mAssetReader release];
    [mAssetReaderOutput release];
    NSLog(@"count: %d", [images count]);
}

当while循环执行时,它打印我放入的日志,增加整数val。 在这之间有一些消息"收到内存警告"在GDB中显示。

非常感谢...

2 个答案:

答案 0 :(得分:2)

听起来你需要考虑在自动释放池https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html中包装while循环的每次迭代

答案 1 :(得分:0)

移动你的

  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

直接在while()循环内部,然后放一个

  [pool drain];

就在while循环结束之前。

此外,根据您拥有的图像数量,您将耗尽内存,因为您要保留在内存中缩小的图像...

相关问题