gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:14:21 27 4
gpt4 key购买 nike

我正在创建一个每秒拍摄一张照片并将图像保存到磁盘的应用程序。当拍摄了足够多的照片后,该应用程序会使用 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 没有改变任何东西。我尝试编写自定义图层并自己绘制图像。这确实允许我渲染更多图像,但最终仍然会发出内存警告并使应用程序崩溃。事实证明,除非在图层上调用 -setNeedsDisplay,否则不会调用 -drawInContext:。该类如下所示:

PhotoLayer.h

#import <QuartzCore/QuartzCore.h>

@interface PhotoLayer : CALayer

@property (nonatomic, copy) NSString *path;

@end

照片图层.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];

最佳答案

当 imagePath 的数量很大时,对于每个 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,您就会泄漏原始图像,这就是您的问题

关于iphone - 使用图像创建图层时内存警告会导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17381712/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com