Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a UIImage with UIBezierPath

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?

-(UIImage*) drawTriangle{
    CGRect rect = CGRectMake(0.0, 0.0, 30.0, 30.0);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIGraphicsPushContext(context);
    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;
}
like image 915
zedzhao Avatar asked Jul 01 '13 15:07

zedzhao


People also ask

What is a UIBezierPath object?

A UIBezierPath object combines the geometry of a path with attributes that describe the path during rendering. You set the geometry and attributes separately and can change them independent of one another. After you have the object configured the way you want it, you can tell it to draw itself in the current context.

How do I draw a béZier path?

After configuring the geometry and attributes of a Bézier path, you draw the path in the current graphics context using the stroke () and fill () methods. The stroke () method traces the outline of the path using the current stroke color and the attributes of the Bézier path object.

Can I use béZier path objects multiple times?

Because the creation, configuration, and rendering process are all distinct steps, Bézier path objects can be reused easily in your code. You can even use the same object to render the same shape multiple times, perhaps changing the rendering options between successive drawing calls.


2 Answers

extension UIBezierPath {
    func shapeImage(view: UIView) -> UIImage! {
    
    // begin graphics context for drawing
    UIGraphicsBeginImageContextWithOptions(view.frame.size, false, UIScreen.main.scale)
    
    // configure the view to render in the graphics context
    //view.layer.render(in: UIGraphicsGetCurrentContext()!)
    
    // get reference to the graphics context
    let context = UIGraphicsGetCurrentContext()
    
    // translate matrix so that path will be centered in bounds
    context?.translateBy(x: -(bounds.origin.x - self.lineWidth), y: -(bounds.origin.y - self.lineWidth))
    // set color
    UIColor.black.setStroke()
    
    // draw the stroke
    stroke()
    
    // get an image of the graphics context
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    
    // end the context
    UIGraphicsEndImageContext()
    
    return image
    }
}
like image 185
Leonif Avatar answered Oct 26 '22 06:10

Leonif


https://stackoverflow.com/a/17408397/3191351 Translated Into Swift

import UIKit

extension UIBezierPath {

    /** Returns an image of the path drawn using a stroke */
    func strokeImageWithColor(var strokeColor: UIColor?,var fillColor: UIColor?) -> UIImage {

        // get your bounds
        let bounds: CGRect = self.bounds

        UIGraphicsBeginImageContextWithOptions(CGSizeMake(bounds.size.width + self.lineWidth * 2, bounds.size.width + self.lineWidth * 2), false, UIScreen.mainScreen().scale)

        // get reference to the graphics context
        let reference: CGContextRef = UIGraphicsGetCurrentContext()!

        // translate matrix so that path will be centered in bounds
        CGContextTranslateCTM(reference, self.lineWidth, self.lineWidth)

        if strokeColor == nil {
            strokeColor = fillColor!;
        }
        else if fillColor == nil {
            fillColor = strokeColor!;
        }

        // set the color
        fillColor?.setFill()
        strokeColor?.setStroke()

        // draw the path
        fill()
        stroke()


        // grab an image of the context
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        return image
    }

}
like image 38
jrisberg Avatar answered Oct 26 '22 06:10

jrisberg