xcode IOS:在视图中绘制图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10138649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:57:52  来源:igfitidea点击:

IOS: draw an image in a view

iosxcodeuiimagepng

提问by cyclingIsBetter

I have this code to color in a view:

我有这个代码在视图中着色:

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

    UIGraphicsBeginImageContext(drawImage.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 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();

my problem is that I want to color not with a point, but I want to use a particular image that is repeated (as a png image) Is it possible?

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

回答by Dondragmer

It's easy to load a single UIImage and draw it:

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

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

This will draw the image just once per cycle, not a continuous line. If you want solid lines of your brush picture, see Objective C: Using UIImage for Stroking.

这将每个周期只绘制一次图像,而不是一条连续的线。如果您想要画笔图片的实线,请参阅目标 C:使用 UIImage 进行描边

This could end up loading the image file every time this method is called, making it slow. While the results of [UIImage imageNamed:]are often cached, my code above could be improved by storing the brush for later reuse.

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

Speaking of performance, test this on older devices. As written, it worked on my second generation iPod touch, but any additions could make it stutter on second and third generation devices. Apple's GLPaintexample, recommended by @Armaan, uses OpenGL for fast drawing, but much more code is involved.

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

Also, you seem to be doing your interaction (touchesBegan:, touchesMoved:, touchesEnded:) in a view that then draws its contents to drawImage, presumably a UIImageView. It is possible to store the painting image, then set this view's layer contents to be that image. This will not alter performance, but the code will be cleaner. If you continue using drawImage, access it through a propertyinstead using its ivar.

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

回答by Iducool

You can start with APPLE's sample code:Sample code:GLPaint

可以从APPLE的示例代码开始:Sample code:GLPaint