2013-07-22 27 views
0

我在UIImageView中绘制图像顶端时遇到了困难,我已经看过"Draw another image on a UIImage"但是它只有这么多的帮助。这里是我的场景:我有一个UIPopoverController,里面有一个UITableView,当用户位于可滚动表格视图底部时,我想显示一个三角形。如何在UIImageView中通过UIImage绘制三角形

我的代码:

- (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; 
} 

我不能完全似乎提请的三角形的UIImage在ImageView的,最终的结果要么是完全黑色的ImageView(该UIImage的仅仅是一个白色的或者在图像视图顶部有一行(取决于我使用的是drawInRect:还是drawAtPoint:以及我使用的坐标)。我所要做的就是绘制一个三角形,而不是一个硬的东西,而且看起来我真的很喜欢这本书。“

+0

我不确定,但我认为你必须将三角形放在它自己的视图中并将其添加到imageView中。 – Jamie

+0

谢谢,我会研究一下。 – Sakiboy

回答

1

你的代码看起来太复杂了,对于那种任务。你为什么不使用简单的UIImageView与任何你想要的三角形图像?只需在桌子底部显示即可。

请注意,当您在执行UITableView的代理时,您还可以看到它的UIScrollView的代表消息,如scrollView:didEndDecelerating:等。

关于你的代码,你已经错过了一个实际的路径抽奖:

CGContextAddPath(context, trianglePath); 
CGContextFillPath(context); 

为了能够你应该使用路径相关的功能。使用CGContext相关函数似乎是不正确的,因为这种方法可能会删除旧的路径。所以更好的方法是在上下文中创建自己的可变路径,在其中绘制三角形,然后通过上述函数将此路径绘制到上下文中。

+0

你能否解释一下如何使用'CGContextAddPath(context,trianglePath);'?在几次我不得不使用CGContext操作绘制时,我从来没有使用'CGContextAddPath()'(我只有使用CGContext绘制线条和矩形到pdf的经验。)。感谢您的回复,如果我无法得到这个工作,那么我会添加一个简单的triangle.png到我的imageView。我只想要一些CG绘图的经验。 – Sakiboy

+0

我现在看到如何使用CGContextAddPath(),并意识到我忘记了添加 “CGContextDrawPath(ctx,kCGPathFillStroke);”在我的代码中也...谢谢你的回答。 – Sakiboy

2

这是我完成的代码在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; 

}