All the iOS drawing information I'm finding is related to how to display things in a view. Specifically layering different images, rects, or paths in a view to be seen on screen. I'm having trouble finding information as to how I can actually manipulate a UIImage itself.
我发现的所有iOS绘图信息都与如何在视图中显示东西有关。在屏幕上可以看到不同的图像、矩形或路径。我很难找到关于如何操作UIImage本身的信息。
Specifically, I'm looking to open an image file as a UIImage (already know how to do this), draw a line in that image, and then save the new UIImage to a file (already know how to do this as well). I would assume there would be a fairly simple way to do this, but every solution I find seems to be going into a complex solution which seems like overkill for what I assume would be a minor task. How can I go about doing this simply? Or are the much more complex solutions the necessary way?
具体来说,我希望打开一个图像文件作为一个UIImage(已经知道如何做到这一点),在该图像中画一条线,然后将新的UIImage保存到一个文件中(已经知道该如何做了)。我认为这是一个相当简单的方法,但是我发现的每一个解决方案似乎都是一个复杂的解决方案,我认为这是一个小任务。我怎么能简单地做这件事?或者更复杂的解决方案是必要的吗?
I'm guessing something similar to this has been answered before, but my lack of knowledge in the area seems to be making it difficult to properly search for and find these answers.
我猜类似的事情以前也有人回答过,但是我在这个领域缺乏知识似乎很难找到答案。
2 个解决方案
#1
0
from & to coordinates are in image pixel units
从&到坐标是在图像像素单位。
func drawLineOnImage(size: CGSize, image: UIImage, from: CGPoint, to: CGPoint) -> UIImage {
UIGraphicsBeginImageContext(size)
image.drawAtPoint(CGPointZero)
let context = UIGraphicsGetCurrentContext()
CGContextSetLineWidth(context, 1.0)
CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor)
CGContextMoveToPoint(context, from.x, from.y)
CGContextAddLineToPoint(context, to.x, to.y)
CGContextStrokePath(context)
let resultImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return resultImage
}
you can fine the Objective-c answer Here
你可以在这里写出Objective-c的答案。
#2
0
Updated for Swift 4 from answer by Nadeesha Lakmal
通过Nadeesha Lakmal的回答更新了Swift 4。
func drawLineOnImage(size: CGSize, image: UIImage, from: CGPoint, to: CGPoint) -> UIImage {
UIGraphicsBeginImageContext(size)
image.draw(at: CGPoint.zero)
guard let context = UIGraphicsGetCurrentContext() else { return UIImage() }
context.setLineWidth(1.0)
context.setStrokeColor(UIColor.blue.cgColor)
let startPoint = CGPoint(x: from.x, y: from.y)
let endPoint = CGPoint(x: to.x, y: to.y)
context.move(to: startPoint)
context.addLine(to: endPoint)
context.strokePath()
guard let resultImage = UIGraphicsGetImageFromCurrentImageContext() else { return UIImage() }
UIGraphicsEndImageContext()
return resultImage
}
#1
0
from & to coordinates are in image pixel units
从&到坐标是在图像像素单位。
func drawLineOnImage(size: CGSize, image: UIImage, from: CGPoint, to: CGPoint) -> UIImage {
UIGraphicsBeginImageContext(size)
image.drawAtPoint(CGPointZero)
let context = UIGraphicsGetCurrentContext()
CGContextSetLineWidth(context, 1.0)
CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor)
CGContextMoveToPoint(context, from.x, from.y)
CGContextAddLineToPoint(context, to.x, to.y)
CGContextStrokePath(context)
let resultImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return resultImage
}
you can fine the Objective-c answer Here
你可以在这里写出Objective-c的答案。
#2
0
Updated for Swift 4 from answer by Nadeesha Lakmal
通过Nadeesha Lakmal的回答更新了Swift 4。
func drawLineOnImage(size: CGSize, image: UIImage, from: CGPoint, to: CGPoint) -> UIImage {
UIGraphicsBeginImageContext(size)
image.draw(at: CGPoint.zero)
guard let context = UIGraphicsGetCurrentContext() else { return UIImage() }
context.setLineWidth(1.0)
context.setStrokeColor(UIColor.blue.cgColor)
let startPoint = CGPoint(x: from.x, y: from.y)
let endPoint = CGPoint(x: to.x, y: to.y)
context.move(to: startPoint)
context.addLine(to: endPoint)
context.strokePath()
guard let resultImage = UIGraphicsGetImageFromCurrentImageContext() else { return UIImage() }
UIGraphicsEndImageContext()
return resultImage
}