Search code examples
xcode6drawingalphauigraphicscontext

change alpha of a UIImage then use as colorWithPatternImage


I have a drawing app that I use preexisting .png images to draw certain textured colors into a UIImageView.

//basic drawing stuff

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   ctr = 0;
    pts[0] = [touch locationInView:self.tempImage];

    lastPoint = [touch locationInView:tempImage];
    }


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [touches anyObject];
            CGPoint p = [touch locationInView:self.tempImage];
    currentPoint = [touch locationInView:tempImage];
    ctr++;
    pts[ctr] = p;

    if (ctr == 4)

    {
        pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment

        [path moveToPoint:pts[0]];
        [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]

    [self draw2];
 pts[1] = pts[4];
        ctr = 1;
           }
            lastPoint = currentPoint;
    }

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    [path removeAllPoints];
           ctr = 0;
           UIGraphicsBeginImageContext(self.tempImage.frame.size);
     [self.imagesView.image drawInRect:CGRectMake(0,0, self.imagesView.frame.size.width, self.imagesView.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
    [self.tempImage.image drawInRect:CGRectMake(0,0, self.tempImage.frame.size.width, self.tempImage.frame.size.height) blendMode:kCGBlendModeNormal alpha:a];
     self.imagesView.image = UIGraphicsGetImageFromCurrentImageContext();
           self.tempImage.image = nil;
           UIGraphicsEndImageContext();
  }


- (void)draw2
{
UIGraphicsBeginImageContext(self.tempImage.frame.size);
        [self.tempImage.image drawInRect:CGRectMake(0, 0, self.tempImage.frame.size.width, self.tempImage.frame.size.height)];
 if ([pencilString isEqualToString:@"red"]) {

//I assign the preexisting .png (RedTextureImage.png) to the colorWithPatternImage and draw with it

brushImage = [UIImage imageNamed:@"RedTextureImage.png];

                [[UIColor colorWithPatternImage:brushImage]setStroke];
            }
 CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
            [path stroke];

        }

//and this sets its alpha

 self.tempImage setAlpha:a];
 UIGraphicsEndImageContext();
 }

so I can draw a stroke or line across the tempView when touchesMoved

and then have it saved onto the imagesView on touchesEnd at whatever alpha I choose.

PROBLEM

What I want to do is take the imagesView.image and save it as a png image and load it into the colorWithPatternImage, changing its alpha at will. So I can draw a line across the original drawing that is more transparent and then change the alpha and have a different line that is of a different transparency.

How do I save a UIImageView.image as a .png image to be used for the colorPatternImage?

I have an image to illustrate but can't see how to upload it.


Solution

  • So I figured this out:

    In TouchesBegan I made brushImage = self.imagesView.image;

    Then in TouchesMoved I used code to erase the stroke form imagesView and (draw2) redraw it using colorWithPatternColor brushImage but instead of using blendModeNormal I used blendModeCopy.

    And I works just the way I want.