【问题标题】:Memory warning results in crash when creating layers with images使用图像创建图层时,内存警告会导致崩溃
【发布时间】:2013-06-27 05:32:19
【问题描述】:

我正在创建一个应用程序,它每秒拍摄一张照片并将图像保存到磁盘。 当拍摄了足够多的照片后,该应用程序会使用 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 的回答。

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

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

【问题讨论】:

    标签: iphone objective-c avfoundation calayer


    【解决方案1】:

    当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,这就是您的问题

    【讨论】:

    • 自动释放池没有改变任何东西。我尝试自己将图像绘制到图层上。这确实允许我渲染更多图像,但最终我会收到内存警告并且应用程序将崩溃。事实证明,除非在层上调用-setNeedsDisplay,否则不会调用-drawInContext:。我更新了我的问题以显示我如何实现 CALayer 子类。请,如果你有任何其他想法,让我在这里。我真的被这个问题困住了。
    • 我的代码显然有问题。 Instruments 表明,在创建层时,应用程序会使用越来越多的内存。我想这不是预期的。在图像上,小尖峰是拍摄图像的时候。然后渲染阶段开始,应用程序继续使用内存。 c.4su.re/PyIE
    • 似乎是CGContextDrawImage 没有释放它的内存。这听起来对吗?有什么办法可以避免这种情况吗?
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-28
      相关资源
      最近更新 更多