ZhangChn
ZhangChn

Reputation: 3184

CGPathCreateCopyByStrokingPath equivalent on iOS4?

I found CGPathCreateCopyByStrokingPath on iOS 5.0 quite convenient to use but it is available on iOS 5 and later.

Is there any simple way to achieve the same path copying on iOS 4?

Upvotes: 2

Views: 1167

Answers (2)

Adam
Adam

Reputation: 33126

I use this, which is compatible across IOS5 and IOS4+. It works 100% if you use the same fill + stroke color. Apple's docs are a little shady about this - they say "it works if you fill it", they don't say "it goes a bit wrong if you stroke it" - but it seems to go slightly wrong in that case. 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();
    }
}

Upvotes: 3

jstevenco
jstevenco

Reputation: 2953

Hmmm -- don't know if this qualifies as "simple", but check out Ed's method in this SO post.

Upvotes: 1

Related Questions