0

我有这段代码可以获取两个不同视频中的两个视频帧。我将它们合并成单帧,然后用合并的帧创建电影。但问题是由于内存警告导致应用程序崩溃。这是我的代码:

NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"/Documents/movie.mp4"]];
NSURL *outputURL = [NSURL fileURLWithPath:filePath];
player = [[MPMoviePlayerController alloc]initWithContentURL:outputURL];
float frame = 0.00;
int count = 10;  

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
docPath = [docPath stringByAppendingPathComponent:@"OutPut"];
BOOL success = [fileManager fileExistsAtPath:docPath];

if (success) {
    [fileManager removeItemAtPath:docPath error:nil];
}

[fileManager createDirectoryAtPath:docPath withIntermediateDirectories:YES attributes:nil error:nil];

for (frame = (frameStartTime); frame < (frameStartTime+5); frame+=0.033) {
    UIImage * singleFrameImage = [player thumbnailImageAtTime:frame timeOption:MPMovieTimeOptionExact];
    [player pause];
    NSString *imageName = [NSString stringWithFormat:@"export2%d.png",count];
    NSString * file = [[NSBundle mainBundle]pathForResource:imageName ofType:nil];
    UIImage *overlayImage = [UIImage imageWithData:[NSData dataWithContentsOfFile:file]];

    count = count + 1;
    NSString *imagePath = [NSString stringWithFormat:@"%@/%@", docPath, imageName];
    if (overlayImage) {
        UIImage *  outImage = [self mergeImage:singleFrameImage withImage:overlayImage];
        NSData *imgData = [[NSData alloc] initWithData:UIImagePNGRepresentation(outImage)];
        [fileManager createFileAtPath:imagePath contents:imgData attributes:nil];
        [imgData release];
    }
    else {
        NSData *imgData = UIImagePNGRepresentation(singleFrameImage);
        [fileManager createFileAtPath:imagePath contents:imgData attributes:nil];               
    }
    [outputFramesArray addObject:imagePath];
}

[player release];

if([fileManager fileExistsAtPath:filePath]) {
    [fileManager removeItemAtPath:filePath error:nil];    
}

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"/Documents/movie1.mp4"]];
NSLog(@"filePath %@", path);

if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
    [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}

[self writeImageAsMovie:outputFramesArray toPath:path size:CGSizeMake(480, 320) duration:10];
NSLog(@"hello Your layering is completed");
[outputFramesArray removeAllObjects];
[outputFramesArray release];

工具中的分配在此行中最大 (90%):

NSData *imgData = [[NSData alloc] initWithData:UIImagePNGRepresentation(outImage)];

在我的整个应用程序中,仅此代码点的分配级别就达到了 130MB。

你们中的任何人都可以提出任何解决方案吗?

4

1 回答 1

1

这很可能是因为您在循环中消耗了内存,并且当前堆栈中分配的内存变得太大,然后您才返回到取消分配的主循环。

阅读这篇博文了解更多详情。

做这个:

...
for (frame = (frameStartTime); frame < (frameStartTime+5); frame+=0.033)
{
    @autoreleasepool {
        // Do whatever you do in your for loop...
    }
}
...

它将创建一个本地池,在每次迭代时都会被耗尽。

于 2012-12-20T13:04:48.577 回答