使用UIBezierPath绘制画笔正在使用uiview但不能使用uiimageview?

时间:2012-04-17 09:26:42

标签: iphone uiview uiimageview uibezierpath

我正在使用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方法,但它在屏幕上不显示任何内容。有谁知道这有什么问题或我该如何解决这个问题?

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

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

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

2 个答案:

答案 0 :(得分:2)

来自UIImageView文档:

  

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

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

答案 1 :(得分:0)

可以使用以下代码完成:

@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;     
}