簡體   English   中英

UIImageView使用圖像的大小而不是圖像視圖來繪制矩形

[英]UIImageView draw in rect with size of image and not image view

我想在UIImageView上進行繪制,以下是我的繪制代碼。 我的圖像查看模式適合寬高比。 當我使用以下代碼進行繪制時,由於我正在使用drawinrect並提供UIImageView的rect,因此所有圖像均按比例縮小到`imageview的大小。 我想繪制的圖像大小為圖像大小而不是圖像視圖。由於我正在檢測圖像視圖上的接觸點,因此圖像上的點擊和實際繪制點有所不同。 誰能告訴我如何直接在圖像上繪畫。 以及如何計算圖像(而非圖像視圖)上的觸摸點的這種偏移或差異。

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

    UITouch *touch = [touches anyObject];
    lastTouch = [touch locationInView:self];

 }

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

CGPoint   currentTouch = [[touches anyObject] locationInView:self];

UIGraphicsBeginImageContext(self.image.size);
CGContextRef context = UIGraphicsGetCurrentContext();

[self.image drawInRect:CGRectMake(0, 0, self.image.size.width, self.bounds.size.height)];

CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, _brushSize);

CGContextBeginPath(context) ;

CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
CGContextAddLineToPoint(context,currentTouch.x,currentTouch.y) ;

CGContextSetAlpha( context , 1 );

CGContextSetStrokeColorWithColor(context, [_brushColor CGColor]);

CGContextStrokePath(context) ;

self.image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

lastTouch = currentTouch;


 }

您不應該真的對我們分類UIImageView 它不打算被子類化。

更好的方法是使用UIView而不是UIImageView

那么兩種可能的方法是...

UIImage直接繪制到drawRect內部的UIView ,但這將給您當前遇到的相同問題。

要么...

UIView有一個UIImageView 調整大小,使其是UIImage的正確大小/形狀。 (即自行縮放),然后在UIImageView放置第二個UIView (並將其子類化),第二個UIView會以DrawingView或其他內容為上限。 現在,您可以在此處進行所有繪圖。 您的所有觸摸檢測也將在這里。 因此,您不再需要將接觸點轉換為不同的繪圖點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM