自定义 CALayer - 无效上下文 0x0

2024-05-10

我正在尝试构建一个使用图层的应用程序,我的应用程序结构是

UIView --> UIScrollView --> UIView --> LayersView (Custom UIView)
                                   --> UIImageView

我想向 LayersView 添加多个图层,所以我构建了一个自定义CALayer那个使用UIBezierPath绘制一组点。

CALayerBezierPath.h

#import <QuartzCore/QuartzCore.h>

@interface CALayerBezierPath : CALayer {
    NSMutableArray *pointsArray;
}
@property (nonatomic, retain) NSMutableArray *pointsArray;

- (void) initVariables;
- (void) addNewPoints:(CGPoint)newPoint;
@end

CALayerBezierPath.m

#import "CALayerBezierPath.h"

@implementation CALayerBezierPath

@dynamic pointsArray;

-(void)drawInContext:(CGContextRef)ctx {
    NSLog(@"CALayerBezierPath - drawInContext");
    if ([pointsArray count] > 0) {
        UIColor *color = [UIColor redColor];
        [color set];
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:[[pointsArray objectAtIndex:0] CGPointValue]];
        for (int x = 1; x < [pointsArray count]; x++) {
            [path addLineToPoint:[[pointsArray objectAtIndex:x] CGPointValue]];
        }
        [path closePath]; // Implicitly does a line between p4 and p1
        [path fill]; // If you want it filled, or...
        [path stroke]; // ...if you want to draw the outline.
    }
}

-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    NSLog(@"drawLayer");// It's never called, i don't know why
}

- (void) initVariables {
    pointsArray = [[NSMutableArray alloc] init];
}

- (void) addNewPoints:(CGPoint)newPoint {
    [pointsArray addObject:[NSValue valueWithCGPoint:newPoint]];
}

@end

当我在 LayrsView 中启动一个新图层时,我使用以下代码:

    self.layer.backgroundColor = [UIColor whiteColor].CGColor;
    self.layer.cornerRadius = 20.0;
    self.layer.frame = CGRectInset(self.layer.frame, 0, 0);

    for (PlansClass *pclass in layersContent) {
        CALayerBezierPath *sublayer = [CALayerBezierPath layer];
        [sublayer initVariables];

        NSDictionary* json = [pclass.listOfPoints objectFromJSONString];

        float largerX = 0; float largerY = 0;
        float smallerX = 10000; float smallerY = 10000;

        for (NSDictionary *dic in json) {

            [sublayer addNewPoints:CGPointMake([[[json objectForKey:dic] objectForKey:@"x"] floatValue], [[[json objectForKey:dic] objectForKey:@"y"] floatValue])];

            if (largerX < [[[json objectForKey:dic] objectForKey:@"x"] floatValue]) {
                largerX = [[[json objectForKey:dic] objectForKey:@"x"] floatValue];
            }

            if (smallerX > [[[json objectForKey:dic] objectForKey:@"x"] floatValue]) {
                smallerX = [[[json objectForKey:dic] objectForKey:@"x"] floatValue];
            }

            if (largerY < [[[json objectForKey:dic] objectForKey:@"y"] floatValue]) {
                largerY = [[[json objectForKey:dic] objectForKey:@"y"] floatValue];
            }

            if (smallerY > [[[json objectForKey:dic] objectForKey:@"y"] floatValue]) {
                smallerY = [[[json objectForKey:dic] objectForKey:@"y"] floatValue];
            }
        }
        sublayer.frame = CGRectMake(smallerX, smallerY, largerX - smallerX, largerY - smallerY);
        sublayer.backgroundColor = [UIColor redColor].CGColor;
        [self.layer addSublayer:sublayer];
        [sublayer setNeedsDisplay];
    }

问题是每当应用程序午餐时,它都会给我这个错误:

2012-09-13 08:05:47.648 abcd[20744:707] CALayerBezierPath - drawInContext
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetFillColorWithColor: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSaveGState: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetFlatness: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextAddPath: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextDrawPath: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextRestoreGState: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSaveGState: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetLineWidth: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetLineJoin: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetLineCap: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetMiterLimit: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetFlatness: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextAddPath: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextDrawPath: invalid context 0x0
Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextRestoreGState: invalid context 0x0

另外,我也尝试过NSLog the ctx in the drawInContext方法,它给了我这个:

2012-09-13 08:05:47.650 abcd[20744:707] <CGContext 0x15d950>

那么,问题出在哪里,为什么上下文无效,为什么我无法在我的自定义中绘制CALayer?


问题是我没有推送上下文ctx,我正在尝试推送和弹出。所以解决方案很简单,只需将这一行放入 if 语句中drawInContext

UIGraphicsPushContext(ctx);

一切正常。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

自定义 CALayer - 无效上下文 0x0 的相关文章

  • 为什么字符串不等于存储的内容?

    这是一个简单而奇怪的问题 if tableViewNum One if drinkArray objectAtIndex 0 currentDate updatedArray addObject drinkArray NSLog MADE
  • 在 iOS 5 中播放视频

    我正在尝试遵循本教程link http www techotopia com index php Video Playback from within an iOS 5 iPhone Application 但我有问题 有人可以看一下并让我
  • 更改 iOS 中的应用程序语言设置而不是整个设备

    我希望在我的应用程序中可以选择更改我的应用程序语言 只是应用程序语言而不是整个系统 请给我一些提示好吗 提前致谢 我知道本地化 您可以使用从 Xcode 设置的自定义构建标志来完成此操作 这样您就可以在本地化下运行应用程序 而无需更改设备的
  • iOS UILocalNotification - 当应用程序在后台运行并且在通知时单击图标时,不会触发委托方法

    iPhone 版本 5 1 9B176 以下是本地通知期间的一系列事件 其中didFinishLaunchingWithOptions方法未被调用 应用程序正在后台运行 收到本地通知 简单通知 无对话框 单击显示徽章编号的应用程序图标 预计
  • 使用故事板视图控制器创建编程选项卡栏?

    我有一个以编程方式创建的选项卡栏 并且在初始化与视图关联的故事板时遇到困难 我能够在没有故事板的情况下在选项卡栏中成功加载视图 请参阅下面的代码 但视图仅部分显示 因为某些 UI 组件位于故事板中 我的故事板的名称是 Main Storyb
  • 如何区分类实现中两个协议的相同方法名称?

    我有两个协议 protocol P1 void printP1 void printCommon end protocol P2 void printP2 void printCommon end 现在 我在一个类中实现这两个协议 inte
  • 如何将不同层的核心动画一个接一个地链接起来?

    我有一个启用分页的scrollView和N个页面 它们是作为scrollView的子视图的UIView 我正在尝试执行以下操作 用户滚动到第 n 页 此时 之前添加到第 n 页的 7 个 CALayers 即 到页面 scrollView
  • 重复符号_OBJC_CLASS_$_LoginController

    我知道这个问题以前已经被问过很多次了 但到目前为止还没有解决我的问题 我知道当您在项目中获得文件的多个副本时 会发生此错误 我尝试清理构建 删除任何登录控制器 m文件输入编译源然后构建 很好 没有错误 当我在编译源中添加 LoginCont
  • iOS5 上的 UIImagePickerController 内存泄漏

    我在 iOS5 和 XCode4 2 上开发的应用程序中使用 UIImagePickerController 出现内存泄漏 我不知道为什么会出现这种泄漏 您能给我答案吗 和我的代码 void createImagePicker picker
  • iOS后台Location不发送http请求

    我的应用程序需要在后台跟踪用户位置 但无法发送 获取 请求 当应用程序到达前台时 http 请求会立即发送 我正在使用 RestKit 来处理所有网络请求 并且遵循本教程 http www mindsizzlers com 2011 07
  • 检测 UITextField 的焦点变化

    我正在尝试设置当键盘隐藏并出现在文本字段中时视图向上移动的动画 并且它工作得很好 但是当焦点从一个文本字段移动到另一个文本字段时 它不起作用因为键盘已经显示了 在 viewDidLoad 中 我注册了以下内容 NSNotificationC
  • iPhone中的异步for循环

    for循环看起来像这样 我在视图中编写的确实加载了 因此加载此页面需要更多时间 for int i 3 i lt dataDict objectForKey rss objectForKey channel objectForKey ite
  • 从另一个选项卡重新加载 UITableView

    我在尝试重新加载时遇到问题UITableView从 XML 源加载的单元格数据 这是场景 应用程序包含选项卡 其中一个选项卡中有一个表格视图 它从 XML 文件获取数据并且工作正常 但问题是当我想要更改提要类别并从另一个选项卡更改 XML
  • RestKit链接器错误

    我一直遵循 RestKit 安装说明 但现在在尝试构建应用程序时出现错误 这是针对 ios iPad 的 我收到 命令 Developer Platforms iPhoneSimulator platform Developer usr b
  • 在 insertMethod 应用程序中使用 Core Data 会崩溃并给出 NSInternalInconsistencyException 并显示错误消息 Context已经有一个协调器

    我正在 xcode 4 2 中的 insertMethod 在 MasterViewController m 类中 实现一个核心数据示例 我的应用程序崩溃了NSInternalInconsistencyException 和错误消息 Con
  • iOS:提高图像绘制速度

    我有一系列想要制作动画的图像 UIImageView支持一些基本的动画 但不足以满足我的需求 我的第一个方法是使用UIImageView并设置image当图像属性 这太慢了 速度慢的原因是图像的绘制 这让我感到惊讶 我以为瓶颈会加载图像 我
  • 这个错误是无效上下文0x0吗?

    我在ViewDidLoad中编写了以下代码 Implement viewDidLoad to do additional setup after loading the view typically from a nib void view
  • 旋转时自动调整 AVCaptureVideoPreviewLayer 大小的问题

    我编写了一个静态框架 其中有一个名为 ViewfinderViewController 的类 它使用 AVCaptureSession 设置相机 此 ViewController 还将 AVCaptureVideoPreviewLayer
  • iOS5 故事板错误:故事板在 iOS 4.3 及更早版本上不可用

    我使用故事板构建了一个小型应用程序 并且运行得很好 就在最终测试之前 我决定尝试一下它是否可以在 iOS 4 3 上运行 我点击项目设置中灰色的5 0 选择4 3 该应用程序无法构建 并显示以下错误消息 故事板在 iOS 4 3 及更早版本
  • 特别分发:应用程序安装失败

    我已经为我尝试分发的应用程序创建了一个临时存档和一个 ipa 文件 我还创建了一个分发配置文件 其中包含我计划将应用程序分发到的设备的 UDID 当我将 ipa 和 mobileprovision 文件拖到 iTunes 中并尝试将应用程序

随机推荐