iphone - 使用quartz 为 CALayer 绘制内容时发生严重崩溃

标签 iphone memory-management crash quartz-graphics gdc

我试图找出为什么 iOS 以严酷的方式崩溃我的应用程序(没有崩溃日志,立即关闭,黑屏死亡,并显示一段时间的旋转器)。 当我使用 Quartz 为 CALayer 渲染内容时会发生这种情况。我怀疑内存问题(仅在设备上测试时发生),但内存日志以及仪器分配日志看起来相当不错。让我过去的致命函数:

- (void)renderTiles{

    if (rendering) {
        //NSLog(@"====== RENDERING TILES SKIP =======");
        return;
    }
    rendering = YES;



    CGRect b = tileLayer.bounds;
    CGSize s = b.size;
    CGFloat imageScale = [[UIScreen mainScreen] scale];
    s.height *= imageScale;
    s.width *= imageScale;

    dispatch_async(queue, ^{

        NSLog(@"");
         NSLog(@"====== RENDERING TILES START =======");

        NSLog(@"1. Before creating context");
        report_memory();

        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        NSLog(@"2. After creating color space");
        report_memory();

        NSLog(@"3. About to create context with size: %@", NSStringFromCGSize(s));
        CGContextRef ctx = CGBitmapContextCreate(NULL, s.width, s.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);

        NSLog(@"4. After creating context");
        report_memory();

        CGAffineTransform flipTransform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, s.height);
        CGContextConcatCTM(ctx, flipTransform);

        CGRect tileRect = CGRectMake(0, 0, tileImageScaledSize.width, tileImageScaledSize.height);
        CGContextDrawTiledImage(ctx, tileRect, tileCGImageScaled);

        NSLog(@"5. Before creating cgimage from context");
        report_memory();

        CGImageRef cgImage = CGBitmapContextCreateImage(ctx);
        NSLog(@"6. After creating cgimage from context");
        report_memory();

        dispatch_sync(dispatch_get_main_queue(), ^{
            tileLayer.contents = (id)cgImage;        
        });

        NSLog(@"7. After asgning tile layer contents = cgimage");
        report_memory();

        CGColorSpaceRelease(colorSpace);
        CGContextRelease(ctx);
        CGImageRelease(cgImage);

        NSLog(@"8. After releasing image and context context");
        report_memory();


        NSLog(@"====== RENDERING TILES END =======");
        NSLog(@"");
        rendering = NO;

    });
}

以下是日志:

====== RENDERING TILES START =======
1. Before creating context
    Memory in use (in bytes): 28340224 / 519442432 (5.5%)
2. After creating color space
    Memory in use (in bytes): 28340224 / 519442432 (5.5%)
3. About to create context with size: {6324, 5208}
4. After creating context
    Memory in use (in bytes): 28344320 / 651268096 (4.4%)
5. Before creating cgimage from context
    Memory in use (in bytes): 153649152 / 651333632 (23.6%)
6. After creating cgimage from context
    Memory in use (in bytes): 153649152 / 783159296 (19.6%)
7. After asgning tile layer contents = cgimage
    Memory in use (in bytes): 153653248 / 783253504 (19.6%)
8. After releasing image and context context
     Memory in use (in bytes): 21688320 / 651288576 (3.3%)
====== RENDERING TILES END =======

应用程序在随机位置崩溃。有时在达到函数的 en 时,有时在随机步骤中。

我应该向哪个方向寻找解决方案?问题有可能是 GDC 造成的吗?或者可能是上下文大小或一些核心动画底层引用?

最佳答案

经过准确的调试和对渲染上下文大小进行实验后,发现这是与内存相关的问题。解决方案是将上下文的大小限制为最大 2048 宽度和 1024 高度

关于iphone - 使用quartz 为 CALayer 绘制内容时发生严重崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11114746/

相关文章:

iphone - Objective-C 中的标签 <# #>

ios - 我们可以检测哪个 ios 应用程序正在使用我的自定义键盘吗

c - 如何使指向全局结构数组的指针

c - glibc中重复内存分配的效率

windows - 有没有办法将 Windows 崩溃报告重定向到应用程序开发人员?

cocoa-touch - [UIImageView _isChargeEnabled] : message sent to deallocated instance

ios - 通用 API 请求处理程序

iphone - 如何在不使用 AutoLayout 的情况下将 XCode 中 iPhone 4 和 5 的 SubView 修复到屏幕底部?

java - 我的应用程序刚刚崩溃,如何获取崩溃日志?

c++ - 具有动态分配的char数组的C++结构