更改 UIImage 的 alpha 然后用作 colorWithPatternImage
change alpha of a UIImage then use as colorWithPatternImage
我有一个绘图应用程序,我使用预先存在的 .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 上画笔划或线
然后以我选择的任何 alpha 将其保存到 touchesEnd 上的 imagesView 上。
问题
我想要做的是将 imagesView.image 保存为 png 图像并将其加载到 colorWithPatternImage 中,随意更改其 alpha。
所以我可以在原始绘图上画一条更透明的线,然后更改 alpha 并得到一条具有不同透明度的不同线。
如何将 UIImageView.image 保存为 .png 图像以用于 colorPatternImage?
我有一张图片可以说明,但不知道如何上传。
所以我想通了:
在 TouchesBegan 中我制作了 brushImage = self.imagesView.image;
然后在 TouchesMoved 中,我使用代码擦除 imagesView 中的笔划,并使用 colorWithPatternColor brushImage (draw2) 重绘它,但我没有使用 blendModeNormal,而是使用了 blendModeCopy。
而且我按照自己喜欢的方式工作。
我有一个绘图应用程序,我使用预先存在的 .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 上画笔划或线然后以我选择的任何 alpha 将其保存到 touchesEnd 上的 imagesView 上。
问题
我想要做的是将 imagesView.image 保存为 png 图像并将其加载到 colorWithPatternImage 中,随意更改其 alpha。 所以我可以在原始绘图上画一条更透明的线,然后更改 alpha 并得到一条具有不同透明度的不同线。
如何将 UIImageView.image 保存为 .png 图像以用于 colorPatternImage?
我有一张图片可以说明,但不知道如何上传。
所以我想通了:
在 TouchesBegan 中我制作了 brushImage = self.imagesView.image;
然后在 TouchesMoved 中,我使用代码擦除 imagesView 中的笔划,并使用 colorWithPatternColor brushImage (draw2) 重绘它,但我没有使用 blendModeNormal,而是使用了 blendModeCopy。
而且我按照自己喜欢的方式工作。