开发者

How to save a UIBezierPath to an existing UIImageView?

开发者 https://www.devze.com 2023-03-11 07:31 出处:网络
I can\'t figure out how i can save my drawn path to an UIImage? I know that I have to use UIGraphicsBeginImageContext and something like [drawImage drawInRect:CGRectMake(0, 0, drawImage.size.width, dr

I can't figure out how i can save my drawn path to an UIImage?

I know that I have to use UIGraphicsBeginImageContext and something like [drawImage drawInRect:CGRectMake(0, 0, drawImage.size.width, drawImage.size.height)];

But I don't know where to put it in my touch functions.

I need to save every path to the image, because I want to be able to change the color and alpha values after each drawn line.

With this code, all the lines will be adjusted in color and width at the same time when I change the values.

- (id) initWithFrame:(CGRect)frame andImage: (UIImageView*) image
{
    ...
    paths = [[NSMutableArray alloc]init];

    drawImage = image;
    self.backgroundColor=[UIColor clearColor];
    [self addSubview:drawImage];
}
- (void)drawRect:(CGRect)rect
{
    [brushPattern setStroke];
    [myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
    // Drawing code
    //[myPath stroke];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    myPath=[[UIBezierPath alloc]init];
    [myPath moveToPoint:[mytouch locationInView:self]];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    [myPath addLineToPoint:[mytouch locationInView:self]];
    [self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)t开发者_JAVA技巧ouches withEvent:(UIEvent *)event
{

    [paths addObject:myPath];
    [myPath release];
}


Based on your earlier question, I am assuming that the image view is a subview of this custom view object. You will need to maintain a mutable array of paths in this class. Initialize it at touchesBegan:withEvent: and close the path at touchesEnded:withEvent:. Push the path into the array. At every touch event (began, moved, ended), call a method that updates the image view's image after adding the touch point to the path.

Updating the image will involve creating an empty image context and then iterating over all paths in your array and the current path while stroking them. Once you're done drawing, generate the image object from the context and set it to the image. Pretty straightforward until we notice that each path might differ in attributes. To deal with this you should create a container class for the path which will hold the attributes about the path. Push an object of this class for every path into the array rather than the path itself. This way you can maintain state for your view.


There is one way out and very simple infact , each time you draw a path object you can save it in an increamental image , just create it at a place where needed and then show this image in draw rect before drawing something else.

here is the sample code:

  - (void)drawBitmap 
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
    [strokeColor setStroke];
    if (!incrementalImage) // first time draw; paint background white by ...
    {
        UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
        [[UIColor colorWithPatternImage:[UIImage imageNamed:@"two.jpg"]] setFill];
        //[[UIColor whiteColor] setFill];
        [rectpath fill]; // filling it with white
        NSLog(@"========== ... drawBitmap .. CALLED=======");
    }
    [incrementalImage drawAtPoint:CGPointZero];

    for(NSDictionary *_pathDict in pathArray)
    {
        [((UIColor *)[_pathDict valueForKey:@"color"]) setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
        [[_pathDict valueForKey:@"path"] strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
    }
    incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    }

After that just call this increamental image in drawRect: , so that user has a illusion of continuous drawing and you can then save this image point of time:

    - (void)drawRect:(CGRect)rect
{

     [incrementalImage drawInRect:rect];
     [[dict_path objectForKey:@"color"] setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)

     [[dict_path objectForKey:@"path"] strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];

}

May be you need to modify some code according to your own need.Hope this help someone

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号