[objective-c]获取与没有泄漏太多内存分配,应用程序更有可能崩溃在 ipad 上

发布时间: 2016/10/2 3:10:19
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有此代码的两个视频帧每个的两个不同的视频。我将它们合并到单个帧,然后使用合并帧创建一部电影。但问题是由于内存警告的应用程序崩溃。这里是我的代码︰

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)];

在我整个的应用程序,代码只有这点分配水平上升高达 130 MB。

任何你能推荐任何解决办法吗?

解决方法 1:

它是最像,因为你在你的循环中消耗内存和当前堆栈中分配的内存获取太大之前你返回到主回路的东西在哪里得到释放。

阅读此博客文章更多详细信息。

执行此操作︰

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

它将在每次迭代创建一个变得耗尽的本地池。

赞助商