【问题标题】:Drawing paint brush using UIBezierPath is working with uiview but not working with uiimageview?使用 UIBezierPath 绘制画笔可以与 uiview 一起使用,但不能与 uiimageview 一起使用?
【发布时间】:2012-04-17 09:26:42
【问题描述】:

我正在使用 UIBezierPath 使用以下代码制作画笔。

.h File
@interface MyLineDrawingView : UIView
{
    UIBezierPath *myPath;
    UIColor *brushPattern;
}

@end

.m File
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        self.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"0010.png"]];
        myPath=[[UIBezierPath alloc]init];
        myPath.lineWidth=30;
        brushPattern=[UIColor redColor]; 
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [brushPattern setStroke];
    [myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
   // [myPath strokeWithBlendMode:kCGBlendModeSaturation alpha:1.0];

    // Drawing code
    //[myPath stroke];
}

#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{   
    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    [myPath moveToPoint:[mytouch locationInView:self]];   
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    [myPath addLineToPoint:[mytouch locationInView:self]];
    [self setNeedsDisplay];

}

它与UIView 配合得很好。正如您在上面看到的自定义类继承自UIVIew。但是当我继承UIImageView 而不是UIView 时,我无法绘制任何东西。 Toches 方法被调用,但它在屏幕上什么也没画。有谁知道这是什么问题或我该如何解决?

我想将它从UIView 更改为UIImageView 的原因是我想设置图像并更改颜色。当我使用UIView 并使用

self.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"0010.png"]];

图片不适合查看。整个图像只有一部分是可见的。即使我将内容模式更改为UIViewContentModeScaleToFill,整个图像也不可见。

【问题讨论】:

  • 您所说的“使用 uiview 但不使用 uiview 是什么意思?”。 iOS中有多少种UIView?你能帮我吗我只知道一个 UIView

标签: iphone uiview uiimageview uibezierpath


【解决方案1】:

来自 UIImageView 文档:

UIImageView 类经过优化,可以将其图像绘制到显示器上。 UIImageView 不会调用 drawRect: 一个子类。如果您的子类需要自定义绘图代码,建议您使用 UIView 作为基类。

简而言之,在 UIImageView 子类中不能使用 drawRect。将图像视图添加为子视图或将图像绘制为 drawRect 的一部分。

【讨论】:

  • 我也尝试将 imageview 添加为 uiview 的子视图。在这种情况下,结果也相同
  • @Bhoomi:jrturton 的建议应该对你有用。如果它不是你的实现可能需要一些调整。处理此问题的一种不太优雅的方法是制作元视图。首先将 UIImageView 添加到它(作为子视图),然后将您的自定义 UIView 添加到它(也作为子视图)。
【解决方案2】:

可以使用以下代码完成:

@interface Canvas : UIImageView {
    CGPoint location;
}

@property CGPoint location;
.m file
@synthesize location;

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];   
    self.location = [touch locationInView:self];    
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint currentLocation = [touch locationInView:self];  

    UIGraphicsBeginImageContext(self.frame.size); 
    CGContextRef ctx = UIGraphicsGetCurrentContext();  
    //CGContextSetBlendMode(ctx, kCGBlendModeOverlay);


    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    CGContextSetLineWidth(ctx, 5.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    //CGContextSetBlendMode(ctx, kCGBlendModeOverlay);
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, location.x, location.y);
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
    CGContextStrokePath(ctx);
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    location = currentLocation;
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint currentLocation = [touch locationInView:self];

    UIGraphicsBeginImageContext(self.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();  
  //  CGContextSetBlendMode(ctx, kCGBlendModeOverlay);

    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineWidth(ctx, 5.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, location.x, location.y);
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
    CGContextStrokePath(ctx);
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    location = currentLocation;     
}

【讨论】:

    猜你喜欢
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    相关资源
    最近更新 更多