2

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?

ZhangChn
  • 3,154
  • 21
  • 41

2 Answers2

3

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();
    }
}
Adam
  • 32,900
  • 16
  • 126
  • 153
  • Actually, it doesn't seem to be necessary to have a pathFrameRange that is "big enough". In my tests on iOS 4.3, a `CGContext` with a `pathFrameRange` of `CGSizeMake(1,1)` worked fine for a much larger path. – Andreas Ley May 08 '12 at 08:22
1

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

Community
  • 1
  • 1
jstevenco
  • 2,913
  • 2
  • 25
  • 36