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?
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;
}