更改UIImage的alpha然后用作colorWithPatternImage

时间:2015-04-28 05:33:36

标签: xcode6 drawing alpha uigraphicscontext

我有一个绘图应用程序,我使用预先存在的.png图像将某些纹理颜色绘制到UIImageView中。

//基本绘图内容

- (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"]) {

//我将预先存在的.png(RedTextureImage.png)指定给colorWithPatternImage并用它绘制

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

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

        }

//并设置其alpha

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

所以当touchesMoved

时,我可以在tempView上绘制笔划或线条

然后将它保存到touchesEnd上的imagesView中,无论我选择什么alpha。

问题

我想做的是拍摄imagesView.image并将其保存为png图像并将其加载到colorWithPatternImage中,随意更改其alpha值。 因此,我可以在原始图形上绘制一条更透明的线条,然后更改Alpha并使用不同的透明度线条。

如何将UIImageView.image另存为.png图像以用于colorPatternImage?

我有一张图片可供说明,但看​​不到如何上传。

1 个答案:

答案 0 :(得分:0)

所以我想出来了:

在TouchesBegan中,我制作了brushImage = self.imagesView.image;

然后在TouchesMoved中我使用代码擦除笔画形式imagesView和(draw2)使用colorWithPatternColor brushImage重绘它,而不是使用blendModeNormal我使用了blendModeCopy。

我按照我想要的方式工作。