UIGraphics 内存泄漏
UIGraphics Memory Leak
这段代码似乎有内存泄漏,但我找不到它。我究竟做错了什么?具体来说,第 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
没有泄漏;循环的连续迭代不会增加应用程序的内存。
因此我们可以得出结论,如果您的应用程序的内存在增长,那是因为您正在做其他事情(例如您稍后对图像所做的事情)。
这段代码似乎有内存泄漏,但我找不到它。我究竟做错了什么?具体来说,第 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
没有泄漏;循环的连续迭代不会增加应用程序的内存。
因此我们可以得出结论,如果您的应用程序的内存在增长,那是因为您正在做其他事情(例如您稍后对图像所做的事情)。