加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

IOS:在视图中绘制图像

发布时间:2020-12-14 18:08:27 所属栏目:百科 来源:网络整理
导读:我有这个代码在视图中着色: UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:drawImage]; UIGraphicsBeginImageContext(drawImage.frame.size); [drawImage.image drawInRect:CGRectMake(0,drawImage.frame.size.widt
我有这个代码在视图中着色:

UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:drawImage];

    UIGraphicsBeginImageContext(drawImage.frame.size);
    [drawImage.image drawInRect:CGRectMake(0,drawImage.frame.size.width,drawImage.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(),kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(),size);

      CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),r,g,b,a);

    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(),lastPoint.x,lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),currentPoint.x,currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

我的问题是我想要用一个点来着色,但我想使用一个重复的特定图像(作为一个png图像)
可能吗?

解决方法

加载单个UIImage并绘制它很容易:

UIImage *brushImage = [UIImage imageNamed:@"brush.png"];
[brushImage drawAtPoint:CGPointMake(currentPoint.x-brushImage.size.width/2,currentPoint.y-brushImage.size.height/2)];

这将每个周期仅绘制一次图像,而不是连续线.如果您想要画笔的实线,请参阅Objective C: Using UIImage for Stroking.

这可能会在每次调用此方法时最终加载图像文件,从而使其变慢.虽然[UIImage imageNamed:]的结果经常被缓存,但我的代码可以通过存储刷子以便以后重用来改进.

说到性能,请在旧设备上进行测试.正如所写,它适用于我的第二代iPod touch,但任何添加都可能使它在第二代和第三代设备上口吃.由@Armaan推荐的Apple的GLPaint示例使用OpenGL进行快速绘图,但涉及的代码要多得多.

此外,您似乎在视图中进行交互(touchesBegan:,touchesMoved:,touchesEnded :),然后将其内容绘制到drawImage,可能是UIImageView.可以存储绘画图像,然后将此视图的图层内容设置为该图像.这不会改变性能,但代码会更清晰.如果继续使用drawImage,请使用其ivar通过属性访问它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读