gpt4 book ai didi

objective-c:在 UIImage 或 UIImageView 上画线

转载 作者:太空狗 更新时间:2023-10-30 03:57:17 26 4
gpt4 key购买 nike

如何在 UIImage 或 UIImageView 上画一条简单的线?

最佳答案

以下代码的工作原理是创建一个与原始图像大小相同的新图像,将原始图像的副本绘制到新图像上,然后沿着新图像的顶部绘制一条 1 像素的线。

// UIImage *originalImage = <the image you want to add a line to>
// UIColor *lineColor = <the color of the line>

UIGraphicsBeginImageContext(originalImage.size);

// Pass 1: Draw the original image as the background
[originalImage drawAtPoint:CGPointMake(0,0)];

// Pass 2: Draw the line on top of original image
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 1.0);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, originalImage.size.width, 0);
CGContextSetStrokeColorWithColor(context, [lineColor CGColor]);
CGContextStrokePath(context);

// Create new image
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

// Tidy up
UIGraphicsEndImageContext();

或者,您可以将线条创建为CAShapeLayer,然后将其作为 subview 添加到UIImageView(参见this answer)。

关于objective-c:在 UIImage 或 UIImageView 上画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6905941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com