假设 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 次,后台线程泄漏大量。知道为什么吗?