1

我正在维护一家公司的遗留应用程序,并试图减少非 ARC 项目中的崩溃和内存泄漏数量(叹气)。我在将 PDF 文件转换为 UIImage 的函数中发现了大量内存泄漏,但没有足够的图形上下文经验来正确识别所有问题。

泄漏内存的方法如下:

+ (UIImage *) convertPDFToImage:(NSURL *)pdfLocation withResolution:(int)resolution withProductKey:(NSString *) productKey {

CGPDFDocumentRef documentRef = CGPDFDocumentCreateWithURL((CFURLRef)pdfLocation);

if(!CGPDFDocumentIsUnlocked(documentRef))
{
    NSString *hashedPassword = [MD5Hash CreateMD5HashForPDFPasswordUsingProductKey:productKey];
    CGPDFDocumentUnlockWithPassword(documentRef, [hashedPassword UTF8String]);
}

CGPDFPageRef page = CGPDFDocumentGetPage(documentRef, 1); 

CGRect cropBox = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
int pageRotation = CGPDFPageGetRotationAngle(page);

if ((pageRotation == 0) || (pageRotation == 180) ||(pageRotation == -180)) {
    UIGraphicsBeginImageContextWithOptions(cropBox.size, NO, resolution / 72); 
} else {
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(cropBox.size.height, cropBox.size.width), NO, resolution / 72); 
}

CGContextRef context = UIGraphicsGetCurrentContext();  

CGPoint point = CGPointMake(0, 0);
int rotate = CGPDFPageGetRotationAngle(page);
int zoom = 100;
CGContextSaveGState(context);

// Setup the coordinate system.
// Top left corner of the displayed page must be located at the point specified by the 'point' parameter.
CGContextTranslateCTM(context, point.x, point.y);

// Scale the page to desired zoom level.
CGContextScaleCTM(context, zoom / 100, zoom / 100);

// The coordinate system must be set to match the PDF coordinate system.
switch (rotate) {
    case 0:
        CGContextTranslateCTM(context, 0, cropBox.size.height);
        CGContextScaleCTM(context, 1, -1);
        break;
    case 90:
        CGContextScaleCTM(context, 1, -1);
        CGContextRotateCTM(context, -M_PI / 2);
        break;
    case 180:
    case -180:
        CGContextScaleCTM(context, 1, -1);
        CGContextTranslateCTM(context, cropBox.size.width, 0);
        CGContextRotateCTM(context, M_PI);
        break;
    case 270:
    case -90:
        CGContextTranslateCTM(context, cropBox.size.height, cropBox.size.width);
        CGContextRotateCTM(context, M_PI / 2);
        CGContextScaleCTM(context, -1, 1);
        break;
}

// The CropBox defines the page visible area, clip everything outside it.
CGRect clipRect = CGRectMake(0, 0, cropBox.size.width, cropBox.size.height);

CGContextAddRect(context, clipRect);
CGContextClip(context);

CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(context, clipRect);

CGContextTranslateCTM(context, -cropBox.origin.x, -cropBox.origin.y);

CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);

CGContextDrawPDFPage(context, page);    

UIImage *pageImage = UIGraphicsGetImageFromCurrentImageContext(); 

CGContextRestoreGState(context);
UIGraphicsEndImageContext();

CGPDFPageRelease(page);
CGPDFDocumentRelease(documentRef);

return pageImage;
}

我已经解决了一个未发布 CGPDFPage 的泄漏问题,但配置文件显示此方法仍有轻微泄漏。

我们欢迎所有的建议

4

0 回答 0