gpt4 book ai didi

ios - UIGraphics 内存泄漏

转载 作者:行者123 更新时间:2023-11-29 10:21:29 25 4
gpt4 key购买 nike

这段代码似乎有内存泄漏,但我找不到它。我究竟做错了什么?具体来说,第 1 行和第 2 行会增加应用程序的内存使用量,只有第一行的内存会在上下文结束时消失。

UIGraphicsBeginImageContext(imageView.bounds.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

编辑 1:完整方法代码如下:

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(0, 0, self.view.frame.size.width + 16 - ((int)self.view.frame.size.width % 16), self.view.frame.size.height + 16 - ((int)self.view.frame.size.height % 16))]; // If image width and height is not a multiple of 16, the video becomes distorted when written to a file
imageView.contentMode = UIViewContentModeScaleToFill;

[imageView addSubview:[[UIImageView alloc] initWithImage:[ViewController makeRedCircle]]];

UIGraphicsBeginImageContext(imageView.bounds.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

最佳答案

您的代码没有任何内在的泄漏:

UIGraphicsBeginImageContext(imageView.bounds.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

也没有任何泄漏的证据。为了测试,我创建了一个应用程序,其唯一代码如下:

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView* imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
UIImageView* imageView = self.imageView;
for (int i = 0; i <= 10; i++) {
// Here is your code in action!
UIGraphicsBeginImageContext(imageView.bounds.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}

@end

没有泄漏;循环的连续迭代不会增加应用程序的内存。

因此我们可以得出结论,如果您的应用的内存在增长,那是因为您正在做其他事情(例如您稍后对图像所做的事情)。

关于ios - UIGraphics 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34937848/

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