【发布时间】:2015-09-16 13:02:43
【问题描述】:
我有在 UIView 中绘制的图像,然后在它们上绘制线条。如何将带有画线的图像保存到 UIImage 对象中,该对象具有与开始时相同的图像大小,并且不会丢失任何图像质量?
即。我有一张尺寸为(3264、2448)的图像。 这是在 AspectFit 与图像相匹配的 UIView(大小 375、281)中绘制的, 然后在图像上画一条线。最后,如何将 UIView 中的图像保存到大小为 (3264, 2448) 的 UIImage 而不损失图像质量?
如果这不是最好的方法,请推荐一种更好的方法来完成此操作。
class DrawingView: UIImageView {
private var pts = [CGPoint](count: 5, repeatedValue: CGPoint())
private var ctr: uint!
var lineWidth: CGFloat = 4.0
var lineColor: UIColor = UIColor.darkGrayColor()
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init(frame: CGRect, image: UIImage) {
super.init(frame: frame)
self.image = image
beginDrawingView()
}
private func beginDrawingView() {
userInteractionEnabled = true
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
ctr = 0
if let touch = touches.first as? UITouch {
pts[0] = touch.locationInView(self) as CGPoint
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
let p: CGPoint = touch.locationInView(self)
ctr = ctr + 1
pts[Int(ctr)] = p
if ctr == 4 {
pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0);
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
let context = UIGraphicsGetCurrentContext()
image!.drawInRect(CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height))
CGContextSaveGState(context)
CGContextSetShouldAntialias(context, true)
CGContextSetLineCap(context, kCGLineCapRound)
CGContextSetLineWidth(context, lineWidth)
CGContextSetStrokeColorWithColor(context, lineColor.CGColor)
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, pts[0].x, pts[0].y)
CGPathAddCurveToPoint(path, nil, pts[1].x, pts[1].y, pts[2].x, pts[2].y, pts[3].x, pts[3].y)
CGContextSetBlendMode(context, kCGBlendModeNormal)
CGContextAddPath(context, path)
CGContextStrokePath(context)
image = UIGraphicsGetImageFromCurrentImageContext()
CGContextRestoreGState(context)
UIGraphicsEndImageContext()
pts[0] = pts[3]
pts[1] = pts[4]
ctr = 1
}
}
}
}
【问题讨论】:
-
图片来源在哪里?为什么不直接保存源代码?
-
您可以将图片链接保存为字符串或尝试将图片数据保存为NSData格式。
-
您能否在此处分享您的代码,以便在保存图像时更多地了解您在做什么。
-
@u.gen 我希望图像是背景并且可以在其上画线,然后将图像背景和画线保存到最后一张图像。所以我无法保存源代码。
标签: ios iphone image uiview uiimage