【问题标题】:CGPathCreateCopyByStrokingPath equivalent on iOS4?iOS4 上的 CGPathCreateCopyByStrokingPath 等价物?
【发布时间】:2011-10-15 09:19:49
【问题描述】:

我发现 iOS 5.0 上的 CGPathCreateCopyByStrokingPath 使用起来非常方便,但它在 iOS 5 及更高版本上可用。

有没有什么简单的方法可以在iOS 4上实现同路复制?

【问题讨论】:

    标签: ios core-graphics cgpath


    【解决方案1】:

    我使用这个,它兼容 IOS5 和 IOS4+。如果您使用相同的填充 + 描边颜色,它可以 100% 工作。苹果的文档对此有点阴暗——他们说“如果你填满它就可以工作”,他们没有说“如果你抚摸它会有点不对劲”——但在这种情况下似乎有点不对劲。 YMMV。

    // pathFrameRange: you have to provide something "at least big enough to 
    // hold the original path"
    
    static inline CGPathRef CGPathCreateCopyByStrokingPathAllVersionsOfIOS( CGPathRef 
      incomingPathRef, CGSize pathFrameRange, const CGAffineTransform* transform,
      CGFloat lineWidth, CGLineCap lineCap, CGLineJoin lineJoin, CGFloat miterLimit )
    {
        CGPathRef result;
    
        if( CGPathCreateCopyByStrokingPath != NULL )
        {
            /**
            REQUIRES IOS5!!!
             */
            result = CGPathCreateCopyByStrokingPath( incomingPathRef, transform,
                lineWidth, lineCap, lineJoin, miterLimit);
        }
        else
        {
            CGSize sizeOfContext = pathFrameRange;
            UIGraphicsBeginImageContext( sizeOfContext );
            CGContextRef c = UIGraphicsGetCurrentContext();
            CGContextSetLineWidth(c, lineWidth);
            CGContextSetLineCap(c, lineCap);
            CGContextSetLineJoin(c, lineJoin);
            CGContextSetMiterLimit(c, miterLimit);
            CGContextAddPath(c, incomingPathRef);
            CGContextSetLineWidth(c, lineWidth);
            CGContextReplacePathWithStrokedPath(c);
            result = CGContextCopyPath(c);
            UIGraphicsEndImageContext();
        }
    }
    

    【讨论】:

    • 实际上,似乎没有必要拥有一个“足够大”的 pathFrameRange。在我对 iOS 4.3 的测试中,CGContextCGSizeMake(1,1)pathFrameRange 在更大的路径上运行良好。
    【解决方案2】:

    嗯——不知道这是否符合“简单”的条件,但请查看 this SO post. 中 Ed 的方法

    【讨论】:

      猜你喜欢
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多