OGeek|极客世界-中国程序员成长平台

标题: ios - Core Graphics iOS 中的内存增加/泄漏 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:18
标题: ios - Core Graphics iOS 中的内存增加/泄漏

我有一个 UIViewController,里面有一个自定义的 UIView。这个自定义 UIView 使用 drawRect 和 CoreGraphics 绘制一个 pdf。 UIViewController (pdfViewController) 被多次加载:

[self.revealViewController setFrontViewController:[[pdfViewController alloc] initWithPDF:pdfs[indexPath.row] uiColor:[self colorWithRGB:colors[indexPath.row][0]]]];

自定义 UIView 如下所示:

- (void)drawRectCGRect)rect
 {
    [super drawRect:rect];

     NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
     NSURL *url = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponentDF]];

     // Get GraphicsContext
     CGContextRef ctx = UIGraphicsGetCurrentContext();

        // Open PDF Document
        CGPDFDocumentRef pdfDocument =  CGPDFDocumentCreateWithURL((CFURLRef)url);
        CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdfDocument, Page);

            // Get PDF Dimensions
            CGRect cropRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFCropBox);

            // Set white background
            CGContextSetRGBFillColor(ctx, 255.0, 255.0, 255.0, 1.0);
            CGContextFillRect(ctx, rect);

            // Flip Coordinates and reset Origin
            CGContextGetCTM(ctx);
            CGContextScaleCTM(ctx, 1, -1);
            CGContextTranslateCTM(ctx, 0, -rect.size.height);

            // Set render quality
            CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);

            // Scale Matrix
            CGContextScaleCTM(ctx, rect.size.height/cropRect.size.height,rect.size.height/cropRect.size.height);
            CGContextTranslateCTM(ctx, -cropRect.origin.x, -cropRect.origin.y);

            // Draw PDF
            CGContextDrawPDFPage(ctx, pdfPage);

        CGPDFPageRelease(pdfPage);
        CGPDFDocumentRelease(pdfDocument);

}

内存没有被释放,每次加载 UIView 时,UIViewController 内存都会增加 ca。 6MB:/

这是内存使用情况的图片:enter image description here

任何帮助将不胜感激,请随时询问更多信息

更新: drawRect 中的 UIViews 手动内存管理非常好。由于(强)委托(delegate)引用,UIView 本身没有释放自己。再次感谢所有答案



Best Answer-推荐答案


听起来 UIView 本身没有被释放。 (既然我们在评论中弄清楚了,不妨填写答案。)

关于ios - Core Graphics iOS 中的内存增加/泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27462642/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4