How to create a UIImage with UIBezierPath

前端 未结 7 857
南旧
南旧 2020-12-09 06:02

I wrote some code to create a UIImage with UIBezierPath but it didn\'t work.

Can someone help me find out what\'s wrong with my code?

相关标签:
7条回答
  • 2020-12-09 06:32

    I found what's wrong with my code.

    I made a mistake on the code order. Simply move the UIGraphicsBeginImageContext(rect.size) to the appropriate position.

    -(UIImage*) drawTriangle{
        CGRect rect = CGRectMake(0.0, 0.0, 30.0, 30.0);
        CGContextRef context = UIGraphicsGetCurrentContext();
        UIGraphicsPushContext(context);
        UIGraphicsBeginImageContext(rect.size); //now it's here.
        UIBezierPath *bezier = [UIBezierPath bezierPathWithRect:rect];
        [bezier moveToPoint:CGPointMake(25, 5)];
        [bezier addLineToPoint:CGPointMake(5, 15)];
        [bezier addLineToPoint:CGPointMake(25, 25)];
        [bezier setLineWidth:3.0];
        [bezier setLineJoinStyle:kCGLineJoinBevel];
        [bezier stroke];
        CGContextAddPath(context, bezier.CGPath);
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsPopContext();
        UIGraphicsEndImageContext();
        return image;
    }
    
    0 讨论(0)
提交回复
热议问题