【问题标题】:How to draw on UIImageView without affecting the background image?如何在不影响背景图像的情况下在 UIImageView 上绘图?
【发布时间】:2011-11-11 10:06:21
【问题描述】:

我有单视图控制器应用程序。它包含 UIImageView 并在用户触摸时在屏幕上绘制。这部分工作正常。在视图中确实加载了视图控制器的方法,我设置了一个不同的图像作为背景图像。我将该图像视图背景设置为清晰的颜色。现在,当我尝试从 UIImageView 中删除某些内容时,我也会删除视图控制器背景视图。我怎样才能避免这种情况?

这是我的代码 在 viewDidLoad() 中为 viewcontroller 设置背景

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bacground.png"]];
image.backgroundColor = [UIColor clearColor];    

使用 touchesMoved 方法在屏幕上绘图

UIGraphicsBeginImageContext(image.frame.size);
[image.image drawInRect:CGRectMake(0, 0, image.frame.size.width, image.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), self.drawcolor.CGColor);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
image.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

要擦除,我只是将 drawcolor 设置为 whiteColor。

【问题讨论】:

标签: iphone uiimageview core-graphics background-image background-color


【解决方案1】:

将图像设置为背景使用以下,然后尝试在视图上绘制其他图像

            UIImageView *background = [[UIImageView alloc]initWithImage:[UIImage imageNamed: @"backipad.png"]];
self.view = background;

试试上面的,我从来没有遇到过这样的问题,但我认为这对你有用..and

 background.userInteractionEnabled = YES;

【讨论】:

    猜你喜欢
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 2021-01-17
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 2017-03-17
    相关资源
    最近更新 更多