2012-07-23 98 views
0

我想在UIView中繪製圖像之間的圖像(如鏈接圖像..)。我已經使用在2個圖像視圖之間在UIView中繪製一條線

CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 

CGContextSetLineWidth(context, 2.0); 

CGContextMoveToPoint(context, 0,0); //start at this point 

CGContextAddLineToPoint(context, 20, 20); //draw to this point 

CGContextStrokePath(context); 

但是在這裏,我的上下文爲空。我不知道我是否必須導入任何框架或導入任何標題。任何建議或樣本將不勝感激。

在此先感謝。

+1

,你已經寫了這個代碼? – Iducool 2012-07-23 07:45:25

+0

你必須導入Quartz框架,並在頭文件中寫入:'#import ',但如果你沒有導入它,那麼你會得到編譯器錯誤,所以這可能不是問題。 – pasawaya 2012-07-23 07:49:00

+0

我建議你閱讀這篇文章http://stackoverflow.com/questions/5847876/whats-the-best-approach-to-draw-lines-between-views – Peres 2012-07-23 07:54:19

回答

0
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); 
CGContextSetLineWidth(context, 5.0); 
CGContextMoveToPoint(context, 90, 90.0); 
CGContextAddLineToPoint(context, 120.0, 150.0); 
CGContextStrokePath(context); 
0
UIGraphicsBeginImageContext(image_signature.image.size); 
[yourImageView.image drawInRect:CGRectMake(0, 0, yourImageView.image.size.width, yourImageView.image.size.height)]; 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0); 
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0); 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 0, 0); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 20, 20); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 
[yourImageView setImage:UIGraphicsGetImageFromCurrentImageContext()]; 
UIGraphicsEndImageContext(); 

...「yourImageView」是一個imageview,您將繪製線條。我建議你在每個上面的imageview上畫一條線,在最下面,這是一個選項。

+0

對不起朋友..沒有工作對我來說..我已經在ViewDidload中寫了這段代碼..我所需要的只是在兩個圖像視圖之間的一條簡單的線..我不能發佈圖像.. – 2012-07-23 09:11:32

+0

哥們,那個代碼就像一個魅力:使用其中一個imageView作爲「yourImageView」,然後在屏幕上顯示yourImageView之後立即執行該代碼。 – 2012-07-23 09:57:28

1

非常簡單的方法是使用標籤與身高1 :)

UILabel *seperator=[[UILabel alloc]initWithFrame:CGRectMake(0, 53, 233, 1)]; 
seperator.backgroundColor=[UIColor redColor]; 

[loginView addSubview:seperator]; 
+0

感謝您的支持夥計..這裏是我的最終代碼..我已經子類我的UIView和drawRect方法如下.. CGContextRef context = UIGraphicsGetCurrentContext(); \t //帶有白色筆劃顏色的繪圖線 \t CGContextSetRGBStrokeColor(context,1.0,1.0,1.0,1.0); \t //用2.0筆畫寬度繪製它們,以便它們更清晰可見。 \t CGContextSetLineWidth(context,5.0); \t //從左向右畫一條線 \t CGContextMoveToPoint(context,90,90.0); \t CGContextAddLineToPoint(context,120.0,150.0); \t CGContextStrokePath(context); – 2012-07-23 10:10:42

+0

@ user1402675:它取決於你。感謝您的評論。 – 2012-07-23 10:15:43