3

我正在创建一个每秒拍摄一张照片并将图像保存到磁盘的应用程序。当拍摄了足够多的照片时,该应用程序会使用 AVFoundation 框架创建图像视频。这是通过为每个图像创建一个图层来完成的,添加一个动画,在一段时间后隐藏该图层,然后将这些图层组合成一个由AVVideoCompositionCoreAnimationTool. 创建一个AVVideoComposition包含动画工具的文件后,我最终将使用AVAssetExportSession导出一个AVMutableComposition.

但是,在创建图层时,如果我有 50-60 张或更多照片,我的应用程序会发出两个内存警告,然后崩溃。我使用下面的代码来创建图层。这意味着对于 60 张图像,下面的循环将运行 60 次。每个图像一次。使用照片的imageLayer内容创建,将动画应用于图层,然后将图层作为子图层添加到layer.

CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(0.0f, 0.0f, renderSize.width, renderSize.height);
layer.bounds = CGRectMake(0.0f, 0.0f, renderSize.width, renderSize.height);

for (NSString *imagePath in imagePaths)
{
    CMTime endTime = CMTimeAdd(cursorTime, CMTimeMake(1.0f, (CGFloat)fps));

    UIImage *theImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
    CGFloat scaleFactor = MIN(renderSize.width / theImage.size.width, renderSize.height / theImage.size.height);
    CGSize newSize = CGSizeMake(theImage.size.width * scaleFactor, theImage.size.height * scaleFactor);

    UIImage *resizedImage = [theImage resizedImageToSize:newSize];
    CGImageRef image = resizedImage.CGImage;
    CGSize imageSize = resizedImage.size;

    CALayer *imageLayer = [CALayer layer];
    imageLayer.frame = CGRectMake((renderSize.width - imageSize.width) * 0.50f, (renderSize.height - imageSize.height) * 0.50f, imageSize.width, imageSize.height);
    imageLayer.bounds = CGRectMake((renderSize.width - imageSize.width) * 0.50f, (renderSize.height - imageSize.height) * 0.50f, imageSize.width, imageSize.height);
    imageLayer.contents = (__bridge id)image;
    imageLayer.beginTime = (CGFloat)cursorTime.value / (CGFloat)cursorTime.timescale;

    CABasicAnimation *hideAnimation = [CABasicAnimation animationWithKeyPath:@"hidden"];
    hideAnimation.fromValue = @(NO);
    hideAnimation.toValue = @(YES);
    hideAnimation.additive = NO;
    hideAnimation.removedOnCompletion = NO;
    hideAnimation.beginTime = (CGFloat)endTime.value / (CGFloat)endTime.timescale;
    hideAnimation.fillMode = kCAFillModeBoth;
    [imageLayer addAnimation:hideAnimation forKey:nil];
    [layer addSublayer:imageLayer];

    cursorTime = endTime;
}

似乎是这段代码使用了太多内存。谁能告诉我如何优化代码或以任何其他方式避免内存警告并最终避免崩溃?

每个Daij -Djans 答案的更新。

自动释放池并没有改变任何事情。我尝试编写自定义图层并自己绘制图像。这确实允许我渲染更多图像,但它最终仍然会发出内存警告并使应用程序崩溃。事实证明,-drawInContext:除非-setNeedsDisplay在图层上调用,否则不会调用。该类如下所示:

PhotoLayer.h

#import <QuartzCore/QuartzCore.h>

@interface PhotoLayer : CALayer

@property (nonatomic, copy) NSString *path;

@end

PhotoLayer.m

#import "PhotoLayer.h"
#import <ImageIO/ImageIO.h>

@implementation PhotoLayer

- (void)drawInContext:(CGContextRef)context
{
    [super drawInContext:context];

    // Retrieve image from file path
    CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)[NSData dataWithContentsOfFile:self.path]);
    CGImageRef image = CGImageCreateWithJPEGDataProvider(dataProvider, NULL, TRUE, kCGRenderingIntentDefault);
    CGSize imageSize = CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image));

    // Create transformation for orientation
    CGImageSourceRef source = CGImageSourceCreateWithDataProvider(dataProvider, NULL);
    CFDictionaryRef dictionaryRef = CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
    CGAffineTransform transform;
    NSInteger orientation = [(NSNumber *)CFDictionaryGetValue(dictionaryRef, kCGImagePropertyOrientation) integerValue];
    switch (orientation) {
        case 1:
            // Flip vertically
            transform = CGAffineTransformMake(1.0f, 0.0f, 0.0f, -1.0f, 0.0f, self.bounds.size.height);
            break;
        case 3:
            // Flip horizontally
            transform = CGAffineTransformMake(-1.0f, 0.0f, 0.0f, 1.0f, self.bounds.size.width, 0.0f);
            break;
        case 6:
            // Rotate 90 degrees and flip vertically
            transform = CGAffineTransformMake(0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
            break;
        case 8:
            // Rotate -90 degrees and flip vertically
            transform = CGAffineTransformMake(0.0f, -1.0f, -1.0f, 0.0f, self.bounds.size.height, self.bounds.size.width);
            break;
        default:
            // Unknown orientation, consider landscape right
            transform = CGAffineTransformMake(1.0f, 0.0f, 0.0f, -1.0f, 0.0f, self.bounds.size.height);
            NSLog(@"Unknown orientation when drawing image in layer.");
            break;
    }

    // Calculate amount to scale image
    CGFloat scaleFactor = MIN(self.bounds.size.width / imageSize.width, self.bounds.size.height / imageSize.height);

    // Calculate new size and positiondepending on orientation
    CGSize newSize = CGSizeZero;
    CGPoint pos = CGPointZero;
    if (orientation == 1 || orientation == 3)
    {
        newSize = CGSizeMake(imageSize.width * scaleFactor, imageSize.height * scaleFactor);
        pos = CGPointMake((self.bounds.size.width - newSize.width) * 0.50f, (self.bounds.size.height - newSize.height) * 0.50f);
    }
    else
    {
        imageSize = CGSizeMake(imageSize.height, imageSize.width);
        newSize = CGSizeMake(imageSize.height * scaleFactor, imageSize.width * scaleFactor);
        pos = CGPointMake((self.bounds.size.height - newSize.width) * 0.50f, (self.bounds.size.width - newSize.height) * 0.50f);
    }

    // Apply transformation
    CGContextConcatCTM(context, transform);

    // Draw image
    CGContextDrawImage(context, CGRectMake(pos.x, pos.y, newSize.width, newSize.height), image);

    // Clean up
    CGDataProviderRelease(dataProvider);
    CGImageRelease(image);
    CFRelease(source);
    CFRelease(dictionaryRef);
}

- (void)dealloc
{
    _path = nil;
}

@end

然后我使用下面的图层。我没有设置图层的内容,而是设置了图像的路径并绘制它。要绘制它,我需要调用-setNeedsDisplay.

CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(0.0f, 0.0f, renderSize.width, renderSize.height);
layer.bounds = CGRectMake(0.0f, 0.0f, renderSize.width, renderSize.height);

for (NSString *imagePath in imagePaths)
{
    @autoreleasepool {
        CMTime endTime = CMTimeAdd(cursorTime, CMTimeMake(1.0f, (CGFloat)fps));

        PhotoLayer *photoLayer = [[PhotoLayer alloc] init];
        photoLayer.path = imagePath;
        photoLayer.frame = CGRectMake(0.0f, 0.0f, renderSize.width, renderSize.height);
        photoLayer.bounds = CGRectMake(0.0f, 0.0f, renderSize.width, renderSize.height);
        photoLayer.beginTime = (CGFloat)cursorTime.value / (CGFloat)cursorTime.timescale;

        CABasicAnimation *hideAnimation = [CABasicAnimation animationWithKeyPath:@"hidden"];
        hideAnimation.fromValue = @(NO);
        hideAnimation.toValue = @(YES);
        hideAnimation.additive = NO;
        hideAnimation.removedOnCompletion = NO;
        hideAnimation.beginTime = (CGFloat)endTime.value / (CGFloat)endTime.timescale;
        hideAnimation.fillMode = kCAFillModeBoth;
        [photoLayer addAnimation:hideAnimation forKey:nil];
        [layer addSublayer:photoLayer];

        [photoLayer setNeedsDisplay];

        cursorTime = endTime;
    }
}

CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0.0f, 0.0f, renderSize.width, renderSize.height);
videoLayer.frame = CGRectMake(0.0f, 0.0f, renderSize.width, renderSize.height);
[parentLayer addSublayer:videoLayer];
[parentLayer addSublayer:layer];
4

2 回答 2

0

当 imagePaths 的数量很大时,对于您加载原始的每个 imagePath,制作一个新的调整大小的图像、一个图层和一个动画。

那是你随着时间积累的大量记忆。

  1. 您应该尝试将循环的内容包装在一个@autoreleasepool {...}

如果这还不够,

我会懒惰地加载图像。做一个 CALayer 的子类,将 url 传递给它,然后让它在显示时加载它!(在drawInContext中实现:在您的子类中

就像是

 @MyURLLayer : CALayer
 @property(copy) NSString *path;
 @end

 @implementation MyURLLayer
 - (void)drawInContext:(CGContextRef)ctx {
    //load the image and draw it!
    //...
 }
 @end

顺便说一句,如果您不使用 arc,那么您正在泄漏 originalImage,这就是您的问题

于 2013-06-29T15:53:36.467 回答
0

这永远不会处理大量图像。AVVideoCompositionCoreAnimationTool 需要创建 CAAnimation,然后将引用并保存内存中 CALayer 上的图像数据。在 AVAssetExportSession 取消、失败或完成并被释放(或者您的应用程序更有可能从看门狗关闭)之前,您的所有图像都将在内存中。

不幸的是,这只是对导出会话的限制。有趣的是,AVAsset 在访问之前不会将其数据加载到内存中,但动画工具会这样做。

解决这个问题的唯一方法是使用 AVAssetWriter 之类的东西将图像转码为视频,然后在需要时生成 AVAssetExportSession。

于 2013-08-25T15:47:29.413 回答