0

2 つの異なるビデオのそれぞれ 2 つのビデオ フレームを取得するこのコードがあります。それらを単一のフレームにマージしてから、マージされたフレームでムービーを作成しています。しかし問題は、メモリ警告によるアプリのクラッシュです。これが私のコードです:

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 に答える