iOS - 绘制路径显示图像

时间:2016-04-19 07:54:17

标签: ios objective-c uiimage

我想绘制一条揭示图像的路径。

我设法用这样的渐变来做到这一点:

        CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB();
        CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, colors, NULL, 2);
        CGColorSpaceRelease(baseSpace), baseSpace = NULL;

        CGContextSetLineWidth(context, self.lineWidth);
        CGContextSetLineJoin(context, kCGLineJoinRound);
        CGContextSetLineCap(context, kCGLineCapRound);

        CGContextAddPath(context, path);
        CGContextReplacePathWithStrokedPath(context);
        CGContextClip(context);

        CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
        CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));

        CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
        CGGradientRelease(gradient), gradient = NULL;

我尝试使用相同的原理绘制路径,显示如下图像:

        CGContextSetLineWidth(context, self.lineWidth);
        CGContextSetLineJoin(context, kCGLineJoinRound);
        CGContextSetLineCap(context, kCGLineCapRound);

        CGContextAddPath(context, path);
        CGContextReplacePathWithStrokedPath(context);
        CGContextClip(context);

        CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

        UIImage *image = [UIImage imageNamed:@"placeholder.png"]; <<-- I get EXC_BAD_ACCESS HERE
        CGImageRef imageRef = image.CGImage;
        CGContextDrawImage(context, rect, imageRef);
        CGImageRelease(imageRef), imageRef = NULL;

从上面的代码我EXC_BAD_ACCESS

UIImage *image = [UIImage imageNamed:@"placeholder.png"];

我做错了什么?

1 个答案:

答案 0 :(得分:0)

行。我找到了解决问题的方法。

我改变了

        UIImage *image = [UIImage imageNamed:@"placeholder.png"]; <<-- I get EXC_BAD_ACCESS HERE
        CGImageRef imageRef = image.CGImage;
        CGContextDrawImage(context, rect, imageRef);
        CGImageRelease(imageRef), imageRef = NULL;

        UIImage *image = [UIImage imageNamed:@"placeholder.png"];
        [image drawInRect:rect];