Ios 如何在UIImageView中在UIImage上绘制三角形
我在UIImageView的图像顶部绘制时遇到问题,我已经看过了,但它只是帮了我很大的忙。下面是我的场景:我有一个UIPopoverController,其中有一个UITableView,当用户位于可滚动表视图的底部时,我想显示一个指向上的三角形 我的代码:Ios 如何在UIImageView中在UIImage上绘制三角形,ios,objective-c,uiimageview,uiimage,cgcontextdrawimage,Ios,Objective C,Uiimageview,Uiimage,Cgcontextdrawimage,我在UIImageView的图像顶部绘制时遇到问题,我已经看过了,但它只是帮了我很大的忙。下面是我的场景:我有一个UIPopoverController,其中有一个UITableView,当用户位于可滚动表视图的底部时,我想显示一个指向上的三角形 我的代码: - (UIImage *) drawTriangleInViewForSize:(CGSize)sizeOfView imageToDrawOn:(UIImage *)underImage
- (UIImage *) drawTriangleInViewForSize:(CGSize)sizeOfView
imageToDrawOn:(UIImage *)underImage
isAtTop:(BOOL)top{
CGPoint firstPoint;
CGPoint secondPoint;
CGPoint thirdPoint;
if(!top){
//I want to draw a equilateral triangle (side length 10) in the center of the image in
//the imageView, these are the coordinates I am using.
firstPoint = CGPointMake(underImage.size.width * 0.5 + 5, underImage.size.height * 0.5 - 5);
secondPoint = CGPointMake((firstPoint.x - 10), firstPoint.y);
thirdPoint = CGPointMake(underImage.size.width * 0.5,
underImage.size.width * 0.5 + 5);
}
*/
**DISREGARD**
else{
firstPoint = CGPointMake(sizeOfView.width * 0.5 + 5,
self.tableViewChoices.rowHeight * 0.5 - 5);
secondPoint = CGPointMake((firstPoint.x - 10), firstPoint.y);
thirdPoint = CGPointMake(sizeOfView.width * 0.5,
self.tableViewChoices.rowHeight * 0.5 + 5);
}*/
//get the size of the image for the drawInRect: method
CGFloat imageViewWidth = sizeOfView.width;
CGFloat imageViewHeight = self.tableViewChoices.rowHeight;
//set the graphics context to be the size of the image
UIGraphicsBeginImageContextWithOptions(underImage.size, YES, 0.0);
[underImage drawInRect:CGRectMake(0.0, 0.0, imageViewWidth, imageViewHeight)];
//set the line attributes
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);
CGContextSetFillColorWithColor(ctx, [UIColor clearColor].CGColor);
CGContextSetLineWidth(ctx, 0.05);
UIGraphicsPushContext(ctx);
//draw the triangle
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, firstPoint.x, firstPoint.y);
CGContextAddLineToPoint(ctx, secondPoint.x, secondPoint.y);
CGContextAddLineToPoint(ctx, thirdPoint.x, thirdPoint.y);
CGContextAddLineToPoint(ctx, firstPoint.x, firstPoint.y);
CGContextClosePath(ctx);
UIGraphicsPopContext();
//get the image
UIImage *rslt = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return rslt;
}
我似乎无法在imageView中绘制UIImage的三角形,最终结果要么是一个完全黑色的imageView(UIImage只是一个白色的.png),要么是imageView顶部的一条线(取决于我是使用
drawInRect:
还是drawAtPoint:
以及我使用的坐标)。我所需要做的就是画一个指向上的三角形,这不是一件困难的事情,看起来我真的是在“按部就班地”完成它。你的代码看起来太复杂了,无法完成那种任务。为什么不将simpleUIImageView
用于任何您想要的三角形图像?当桌子放在最下面的时候,把它拿出来就行了
请注意,当您为UITableView
实现委托时,您还能够捕捉到它的UIScrollView
委托消息,例如scrollView:didendreging:
等
关于您的代码,您错过了一个实际的路径图:
CGContextAddPath(context, trianglePath);
CGContextFillPath(context);
要使之成为可能,您应该使用与路径相关的函数。使用
CGContext
相关函数似乎不合适,因为这种方法可能会删除旧路径。因此,更好的方法是在上下文中创建您自己的可变路径,在其中绘制三角形,然后通过上面提到的函数将此路径绘制到上下文中。您的代码对于此类任务来说太复杂了。为什么不将simpleUIImageView
用于任何您想要的三角形图像?当桌子放在最下面的时候,把它拿出来就行了
请注意,当您为UITableView
实现委托时,您还能够捕捉到它的UIScrollView
委托消息,例如scrollView:didendreging:
等
关于您的代码,您错过了一个实际的路径图:
CGContextAddPath(context, trianglePath);
CGContextFillPath(context);
要使之成为可能,您应该使用与路径相关的函数。使用
CGContext
相关函数似乎不合适,因为这种方法可能会删除旧路径。因此,更好的方法是在上下文中创建自己的可变路径,在其中绘制三角形,然后通过上述函数将此路径绘制到上下文中。以下是我在UIImageView中在UIImage上绘制三角形的完整代码
//draws triangle up or down on a UIImage.
+(UIImage *) drawTriangleInViewForSize:(CGSize)sizeOfView
imageToDrawOn:(UIImage *)underImage
isAtTop:(BOOL)top
{
CGFloat rowHeight = underImage.size.height; //44; //self.tableViewChoices.rowHeight;
CGPoint firstPoint;
CGPoint secondPoint;
CGPoint thirdPoint;
CGFloat imageViewWidth = sizeOfView.width;
CGFloat imageViewHeight = rowHeight;
if(!top){
//draw a upward facing triangle in the center of the view.
firstPoint = CGPointMake(imageViewWidth * 0.5 + 15, imageViewHeight * 0.5 - 5);
secondPoint = CGPointMake((firstPoint.x - 30), firstPoint.y);
thirdPoint = CGPointMake(imageViewWidth * 0.5,
imageViewHeight * 0.5 + 5);
}else{
//disregard this 'else'
firstPoint = CGPointMake(sizeOfView.width * 0.5 + 15,
rowHeight * 0.5 - 5);
secondPoint = CGPointMake((firstPoint.x - 10), firstPoint.y);
thirdPoint = CGPointMake(sizeOfView.width * 0.5,
rowHeight * 0.5 + 5);
}
//get the image context with options(recommended funct to use)
//get the size of the imageView
UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageViewWidth, imageViewHeight), YES, 0.0);
//use the the image that is going to be drawn on as the receiver
[underImage drawInRect:CGRectMake(0.0, 0.0, imageViewWidth, imageViewHeight)];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 0.5);
//UIGraphicsPushContext(ctx);
//uses path ref
CGMutablePathRef path = CGPathCreateMutable();
//draw the triangle
CGPathMoveToPoint(path, NULL, firstPoint.x, firstPoint.y);
CGPathAddLineToPoint(path, NULL, secondPoint.x, secondPoint.y);
CGPathAddLineToPoint(path, NULL, thirdPoint.x, thirdPoint.y);
CGPathAddLineToPoint(path, NULL, firstPoint.x, firstPoint.y);
//close the path
CGPathCloseSubpath(path);
//add the path to the context
CGContextAddPath(ctx, path);
CGContextSetFillColorWithColor(ctx, [UIColor whiteColor].CGColor);
CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);
CGContextFillPath(ctx);
CGContextAddPath(ctx, path);
CGContextStrokePath(ctx);
CGPathRelease(path);
//UIGraphicsPopContext();
//get the new image with the triangle
UIImage *rslt = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return rslt;
}
下面是我在UIImageView中在UIImage上绘制三角形的完整代码
//draws triangle up or down on a UIImage.
+(UIImage *) drawTriangleInViewForSize:(CGSize)sizeOfView
imageToDrawOn:(UIImage *)underImage
isAtTop:(BOOL)top
{
CGFloat rowHeight = underImage.size.height; //44; //self.tableViewChoices.rowHeight;
CGPoint firstPoint;
CGPoint secondPoint;
CGPoint thirdPoint;
CGFloat imageViewWidth = sizeOfView.width;
CGFloat imageViewHeight = rowHeight;
if(!top){
//draw a upward facing triangle in the center of the view.
firstPoint = CGPointMake(imageViewWidth * 0.5 + 15, imageViewHeight * 0.5 - 5);
secondPoint = CGPointMake((firstPoint.x - 30), firstPoint.y);
thirdPoint = CGPointMake(imageViewWidth * 0.5,
imageViewHeight * 0.5 + 5);
}else{
//disregard this 'else'
firstPoint = CGPointMake(sizeOfView.width * 0.5 + 15,
rowHeight * 0.5 - 5);
secondPoint = CGPointMake((firstPoint.x - 10), firstPoint.y);
thirdPoint = CGPointMake(sizeOfView.width * 0.5,
rowHeight * 0.5 + 5);
}
//get the image context with options(recommended funct to use)
//get the size of the imageView
UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageViewWidth, imageViewHeight), YES, 0.0);
//use the the image that is going to be drawn on as the receiver
[underImage drawInRect:CGRectMake(0.0, 0.0, imageViewWidth, imageViewHeight)];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 0.5);
//UIGraphicsPushContext(ctx);
//uses path ref
CGMutablePathRef path = CGPathCreateMutable();
//draw the triangle
CGPathMoveToPoint(path, NULL, firstPoint.x, firstPoint.y);
CGPathAddLineToPoint(path, NULL, secondPoint.x, secondPoint.y);
CGPathAddLineToPoint(path, NULL, thirdPoint.x, thirdPoint.y);
CGPathAddLineToPoint(path, NULL, firstPoint.x, firstPoint.y);
//close the path
CGPathCloseSubpath(path);
//add the path to the context
CGContextAddPath(ctx, path);
CGContextSetFillColorWithColor(ctx, [UIColor whiteColor].CGColor);
CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);
CGContextFillPath(ctx);
CGContextAddPath(ctx, path);
CGContextStrokePath(ctx);
CGPathRelease(path);
//UIGraphicsPopContext();
//get the new image with the triangle
UIImage *rslt = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return rslt;
}
我不确定,但我认为你必须将三角形放在它自己的视图中,并将其添加到imageView中。谢谢,我会研究一下。我不确定,但我认为你必须将三角形放在它自己的视图中,并将其添加到imageView中。谢谢,我会研究一下。你能解释一下如何使用“CGContextAddPath(context,trianglePath);”吗??有几次我不得不使用CGContext操作进行绘制,但我从来没有使用过“CGContextAddPath()”(我只有使用CGContext将线条和矩形绘制到pdf的经验)。感谢您的回复,如果我无法实现此功能,那么我只需在imageView中添加一个简单的triangle.png。我只是想要一些CG绘图的经验。我现在看到了如何使用CGContextAddPath(),并且意识到我忘记了在代码中添加“CGContextDrawPath(ctx,kCGPathFillStroke);”。。。谢谢你的回答。你能解释一下如何使用“CGContextAddPath(context,trianglePath);”吗??有几次我不得不使用CGContext操作进行绘制,但我从来没有使用过“CGContextAddPath()”(我只有使用CGContext将线条和矩形绘制到pdf的经验)。感谢您的回复,如果我无法实现此功能,那么我只需在imageView中添加一个简单的triangle.png。我只是想要一些CG绘图的经验。我现在看到了如何使用CGContextAddPath(),并且意识到我忘记了在代码中添加“CGContextDrawPath(ctx,kCGPathFillStroke);”。。。谢谢你的回答。