【问题标题】:transparency on uiimageview should show image [duplicate]uiimageview 上的透明度应显示图像 [重复]
【发布时间】:2012-09-08 10:01:00
【问题描述】:

可能重复:
How to draw a transparent stroke (or anyway clear part of an image) on the iPhone

我有一个透明层的 uiimage 视图 如果我们触摸那个透明层,应该擦除部分透明层并显示它的背面..我该怎么做....? 提前致谢

【问题讨论】:

  • UIImage * backgroundImage = [UIImage imageNamed:@"images.jpeg"]; CALayer* aLayer = [CALayer 层]; CGFloat nativeWidth = CGImageGetWidth(backgroundImage.CGImage); CGFloat nativeHeight = CGImageGetHeight(backgroundImage.CGImage); CGRect startFrame = CGRectMake(0.0, 0.0, nativeWidth, nativeHeight); aLayer.contents = (id)backgroundImage.CGImage; aLayer.frame = startFrame; [self.view.layer addSublayer:aLayer]; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { ;
  • @pammu 你想从源图像中删除所选图像的位吗?
  • 我有一个图像,上面有一个覆盖图像,当我删除覆盖时,它应该显示图像的一部分

标签: iphone objective-c uiimageview


【解决方案1】:

据我了解,您想通过触摸来擦除图像的一部分... 是对的吗 ?

试试这个代码,通过触摸移动部分图像来擦除......

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

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

    CGFloat brushSize = 35;
    CGColorRef strokeColor = [UIColor whiteColor].CGColor;

    UIGraphicsBeginImageContext(scratchView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [canvasView.image drawInRect:CGRectMake(0, 0, canvasView.frame.size.width, canvasView.frame.size.height)];
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, brushSize);
    CGContextSetStrokeColorWithColor(context, strokeColor);
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
    CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
    CGContextStrokePath(context);
    canvasView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastTouch = [touch locationInView:canvasView];
}

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

}

希望对你有帮助....

【讨论】:

    猜你喜欢
    • 2016-01-01
    • 2020-08-31
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 2016-07-04
    相关资源
    最近更新 更多