2014-03-01 27 views
0

我正在使用此raywenderlich教程對圖像進行繪製。該教程是在背景中沒有圖像,但在這裏我想畫畫廊的圖像。圖像在繪製之後在每個觸摸端獲得縮放比例 - iOS

所以基本上有兩個UIImageView一個是和tempDrawImage。 我正在做self.mainImage.image = randomImageFromGallery作爲我想繪製的背景圖片 現在它顯示出來了n繪圖也發生了,但問題是圖像在每個觸摸端都會得到縮放。

爲什麼圖像不斷縮放什麼是錯誤的代碼我沒有做任何事情tempDrawImage所有的繪圖發生?

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    mouseSwiped = NO; 
    UITouch *touch = [touches anyObject]; 
    lastPoint = [touch locationInView:self.view]; 

    UIGraphicsBeginImageContext(self.view.frame.size); 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    mouseSwiped = YES; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.view]; 

    CGContextRef ctxt = UIGraphicsGetCurrentContext(); 
    CGContextMoveToPoint(ctxt, lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(ctxt, currentPoint.x, currentPoint.y); 
    CGContextSetLineCap(ctxt, kCGLineCapRound); 
    CGContextSetLineWidth(ctxt, brush); 
    CGContextSetRGBStrokeColor(ctxt, red, green, blue, 1.0); 
    CGContextSetBlendMode(ctxt,kCGBlendModeNormal); 

    CGContextStrokePath(ctxt); 
    self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    [self.tempDrawImage setAlpha:opacity]; 

    lastPoint = currentPoint; 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesCancelled:touches withEvent:event]; 

    UIGraphicsEndImageContext(); 

    if(!mouseSwiped) 
    { 
     self.tempDrawImage.image = nil; 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    if(!mouseSwiped) { 
     UIGraphicsEndImageContext(); 

     UIGraphicsBeginImageContext(self.view.frame.size); 
     [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush); 
     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity); 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextStrokePath(UIGraphicsGetCurrentContext()); 
     CGContextFlush(UIGraphicsGetCurrentContext()); 
     self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 

    UIGraphicsBeginImageContext(self.mainImage.frame.size); 
    [self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0]; 
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity]; 
    self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    self.tempDrawImage.image = nil; 
    UIGraphicsEndImageContext(); 
} 
+0

每個視圖的框架是什麼? – Wain

+0

@Wain:有兩個UIImageView,正如我在queston中所說的,第一個是mainImage的寬度:316和高度是563,tempDrawImage是316 x 563相同..其中UIImage的原始視圖添加的是320 x 568,默認與視圖控制器 – user1201239

+0

@ Wain:得到了它的提示...謝謝..它的工作現在的問題是兩個框架的大小是不同的,所以它會根據當地的座標規模..謝謝 – user1201239

回答

0

檢查的幀的大小所有您正在使用的視圖。某處出現不匹配的情況,因此您的顯示圖像中的一幅正在縮放顯示。圖像的大小或顯示的大小需要更改。

0

如果我沒有理解好了,你有一個圖片,你想畫了這張圖......

我建議:

  1. 設置你的tempDrawImage從UIView的繼承,而不是UIImageView的。
  2. 存放在陣列中的點,而觸摸操作(的touchesBegan,touchesMoved,...)中的程序(無效)
  3. 平局的drawRect:(的CGRect)RECT