使用图像创建图层时,内存警告会导致崩溃

时间:2013-06-29 15:21:22

标签: iphone objective-c avfoundation calayer

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

更新

autoreleasepool没有改变一件事。我尝试编写自定义图层并自己绘制图像。这确实允许我渲染更多图像,但它仍然会给出内存警告并使应用程序崩溃。 事实证明,除非在图层上调用-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];

2 个答案:

答案 0 :(得分:0)

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

随着时间的推移你会积累很多记忆。

  1. 你应该尝试将循环的内容包装在@autoreleasepool {...}
  2. IF 还不够,

    我懒得加载图片。创建CALayer的子类,将其传递给url,然后在显示时加载它! (在drawInContext中实现:在你的子栏目中

    类似

     @MyURLLayer : CALayer
     @property(copy) NSString *path;
     @end
    
     @implementation MyURLLayer
     - (void)drawInContext:(CGContextRef)ctx {
        //load the image and draw it!
        //...
     }
     @end
    

    顺便说一句,如果你没有使用弧线,你就会泄漏原始图像,这就是你的概率

答案 1 :(得分:0)

这对于大量图像永远不会起作用。 AVVideoCompositionCoreAnimationTool需要创建CAAnimation,CAAnimation将引用并保存内存中CALayer上的图像数据。除非AVAssetExportSession取消,失败或完成并被释放(或者您的应用程序从监视程序关闭,否则更有可能),所有图像都将存储在内存中。

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

解决这个问题的唯一方法是使用类似AVAssetWriter的方式将图像转码为视频,而不是在需要时完成产生AVAssetExportSession的过程。