【发布时间】:2013-05-23 07:43:39
【问题描述】:
假设 UIKit 调用在 iOS 4 中是线程安全的,但是当我尝试在后台线程中呈现 PDF 页面并从视图中拉出图像时,我得到了大量的泄漏。只要我把它放在前台,它就零泄漏。
代码很标准
UIGraphicsBeginImageContext(rect.size);
CGContext ctx = UIGraphicsGetCurrentContext();
// Flip the coordinate system
CGContextTranslateCTM(ctx, 0.0, rect.size.height);
CGContextScaleCTM(ctx, 1.f, -1.f);
// Transform coordinate system to match PDF
NSInteger rotationAngle = CGPDFPageGetRotationAngle(_page);
CGAffineTransform transform = CGPDFPageGetDrawingTransform(_page, kCGPDFCropBox, rect, -rotationAngle, NO);
CGContextConcatCTM(ctx, transform);
CGContextDrawPDFPage(ctx, _page);
UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
如前所述,主线程泄漏 0 次,后台线程泄漏大量。知道为什么吗?
【问题讨论】:
-
你为 bg 线程创建了
NSAutoReleasePool吗? -
是的。我@autoreleasepool {} 整件事。
-
“据说 UIKit 调用在 iOS 4 中是线程安全的”——在发行说明中只有少数是安全的。当然不是全部。
-
@justin:很公平,但我检查了我使用的每一个,并且所有这些都带有“iOS 4 的线程安全”免责声明。
-
@Kalle 对 - 我在示例中没有看到问题,但我想我只是澄清一下以避免任何混淆。如果你没有找到答案,你可以很容易地避免在这个程序中使用 UIKit——只需创建你自己的 CGBitmapContext 并让它创建一个 CGImage。只需添加几行代码。
标签: ios multithreading uikit memory-leaks