2012-06-23 39 views
0

我正在使用名爲Quarkee的應用程序將標誌從SVG轉換爲Quartz 2d代碼,該代碼可以工作。唯一的問題是我似乎無法弄清楚如何調整結果。如果我設置了UIView的框架,drawRect的結果在框架中保持很大。我如何得到它是我設置的框架的大小?IOS Quartz 2D drawRect and Resizing

下面是一個例子。

有人可以幫忙嗎?

- (void)drawRect:(CGRect)rect 
{ 

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGColorSpaceRef colorspace_1 = CGColorSpaceCreateDeviceRGB(); 
CGFloat components_1[] = {0.9961,0.9961,0.9961, 1.0000}; 
CGColorRef color_1 = CGColorCreate(colorspace_1, components_1); 
CGContextSetFillColorWithColor(context,color_1); 

CGContextMoveToPoint(context, 0.4960,0.1090); 
CGContextAddLineToPoint(context,662.9260,0.1090); 
CGContextAddLineToPoint(context,662.9260,227.8780); 
CGContextAddLineToPoint(context,0.4960,227.8780); 
CGContextAddLineToPoint(context,0.4960,0.1090); 
CGContextClosePath(context); 


CGContextFillPath(context); 
+0

說我用自定義drawRect調用我的自定義視圖(如上)UICustomView * myView = [[UICustomView alloc] initWithFrame:CGRectMake(200,70,80,30)];它的框架是正確的,但drawRect中繪製的結果對於框架來說太大了。如何讓drawRect將上下文的大小調整爲給定矩形的大小。 – BigBadOwl

+0

我試過CGContextTranslateCTM(context,rect.size.width,rect.size.height); CGContextScaleCTM(context,1.0,-1.0);例如 – BigBadOwl

+0

也要添加,得到的drawRect是巨大的,有許多路徑,使得改變硬編碼座標變得不可行。 – BigBadOwl

回答

1

這裏的解決方案是使用view.transform = CGAffineTransformMakeScale(0.5f,0.5f);調整視圖的大小

0

這些值請看:

CGContextMoveToPoint(context, 0.4960,0.1090); 
CGContextAddLineToPoint(context,662.9260,0.1090); 
CGContextAddLineToPoint(context,662.9260,227.8780); 
CGContextAddLineToPoint(context,0.4960,227.8780); 
CGContextAddLineToPoint(context,0.4960,0.1090); 

drawRect方法被調用,您可以使用self.bounds把你鑑於當前的矩形,並根據其調整這些值。你說你已經從應用程序中生成了這些代碼 - 這些應用程序通常會在生成代碼時對所繪製圖形的大小進行硬編碼,因此您必須瞭解這些值與您繪製的圖像的實際大小之間的關係,以及使它們根據self.bounds大小動態化...

+0

感謝graver。在這種情況下,drawRect會繪製一個包含數百行代碼的完整標識,並且您的正確,這些標記是硬編碼的,因此將它重寫到提供的矩形的邊界是不可行的。 – BigBadOwl