【发布时间】:2015-08-17 20:14:37
【问题描述】:
我有一个 CGPoint 对象数组。该数组表示一条线。元素 0 是这条线的起点。元素 1 是下一个点,依此类推。我知道我想赋予线条什么颜色和像素粗细。我想最终得到一个包含这一行的 UIImage 。我正在考虑做这样的事情:
// Get graphics context
UIGraphicsBeginImageContextWithOptions(imageSize, false, 1.0)
// Draw line
let line = UIBezierPath()
if let startingPoint = points.firstObject as? CGPoint {
line.moveToPoint(startingPoint)
}
for point in points { // Would I need to ignore the first point?
if let point = point as? CGPoint {
line.addLineToPoint(point)
}
}
// Obtain image and save to file
// End graphics context
UIGraphicsEndImageContext()
这行得通吗?有没有标准或更好的方法来做到这一点?随意在Objective C中回答:)
【问题讨论】:
-
points来自哪里?怎么不是[CGPoint](你得把if let改成CGPoint)? -
points 是从用 obj-C 编写的闭源框架返回的 NSArray。我应该先转换为 [CGPoint] 吗?有什么优势?
标签: ios swift uiimage uibezierpath