renderInContext创建内存泄漏

时间:2015-02-15 06:28:08

标签: ios memory-management memory-leaks core-graphics screenshot

我正在两个视图控制器之间创建自定义转换。要做到这一点,我将抓取视图顶部和底部的自定义区域的屏幕截图,以便为它们设置动画。

但是,每当我执行此操作时,都会创建一个ImageIO_JPEG_Data(每次转换大约7 - 8 mb),从未消除或释放。 (您可以在下面的乐器屏幕截图中看到三个。)

enter image description here

Trolling stackoverflow回答了类似的问题,我试图在dispatch_async(dispatch_get_main_queue(), ^{主线程上明确地运行它,并添加@autorelease池,但无济于事。

我是否遗漏了某些内容,或者是否有更好的方法来创建不会造成内存泄漏的屏幕截图?

//Get Top View
CGSize photoSize = CGSizeMake(self.view.frame.size.width, topHeight);

UIGraphicsBeginImageContextWithOptions(photoSize, NO, 0.0f);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * snapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.layer.contents = nil;

topTransitionView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, snapshot.size.width, snapshot.size.height)];
topTransitionView.image = snapshot;

//Get Bottom View
CGRect photoRect = CGRectMake(0, imageFrame.origin.y + imageFrame.size.height, self.view.frame.size.width,self.view.frame.size.height - (imageFrame.origin.y + imageFrame.size.height));

if (photoRect.size.height <= 0) {
    photoRect.size.height =1;
}

UIGraphicsBeginImageContextWithOptions(photoRect.size, NO, 0.0f);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, -photoRect.origin.x, -photoRect.origin.y);
[self.view.layer renderInContext:ctx];
UIImage *bottomSnapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.layer.contents = nil;

bottomTransitionView = [[UIImageView alloc]initWithFrame:CGRectMake(0, imageFrame.origin.y + imageFrame.size.height, bottomSnapshot.size.width, bottomSnapshot.size.height)];
bottomTransitionView.image = bottomSnapshot;

0 个答案:

没有答案
相关问题