【问题标题】:My ImageView's content mode stop working after I draw something on the image在图像上绘制内容后,我的 ImageView 内容模式停止​​工作
【发布时间】:2016-11-12 23:27:09
【问题描述】:

感谢您的宝贵时间。

在我点击图像之前图像很好,然后在我点击图像后压缩图像。

这是我的代码:

我没有使用情节提要,所以我使用代码创建所有内容,这里是 ImageView。我还用代码添加了约束。

    let imageEditingView: UIImageView = {
    let imageView = UIImageView()
    imageView.contentMode = .scaleAspectFill
    imageView.clipsToBounds = true
    return imageView
}()

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if touches.first != nil {
        lastPoint = (touches.first?.location(in: imageEditingView))!
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    if touches.first != nil {
        let currentPoint = touches.first?.location(in: imageEditingView)
        drawLines(fromPoint: lastPoint, toPoint: currentPoint!)

        lastPoint = currentPoint!
        drawLines(fromPoint: lastPoint, toPoint: lastPoint)
    }
}

func drawLines(fromPoint: CGPoint, toPoint: CGPoint) {
    UIGraphicsBeginImageContext(imageEditingView.frame.size)
    imageEditingView.image?.draw(in: CGRect(x: 0, y: 0, width: imageEditingView.frame.width, height: imageEditingView.frame.height))

    let context = UIGraphicsGetCurrentContext()
    context?.move(to: CGPoint(x: fromPoint.x, y: fromPoint.y))
    context?.addLine(to: CGPoint(x: toPoint.x, y: toPoint.y))
    context?.setBlendMode(CGBlendMode.normal)
    context?.setLineCap(CGLineCap.round)
    context?.setLineWidth(CGFloat(Int(120 * lineWidthSliderView.value)))
    context?.setStrokeColor(red: red / 255, green: green / 255, blue: blue / 255, alpha: 0.01)
    context?.strokePath()

    imageEditingView.image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}    

【问题讨论】:

    标签: ios swift uiimage uigraphicscontext contentmode


    【解决方案1】:

    我不知道您所说的“压缩”是什么意思,但我猜图像会以某种方式损坏,因为当您将图像转换为 CGContext 并重新转换为图像时,一些数据会丢失。

    我不知道如何解决这个问题,但我可以通过将CAShapeLayer 作为子层添加到UIImageView 并以CGPath 的形式在那里绘制你想要的东西来解决这个问题。语法与您现在使用的非常相似,唯一可能不支持的效果是混合模式,但您可以使用具有较低 alpha 值的另一个图层重新创建它。

    这就是它的样子。

    var drawingLayer = CAShapeLayer()
    func drawLines(fromPoint: CGPoint, toPoint: CGPoint) {
        let mutable = CGMutablePath()
        mutable.move(to: fromPoint)
        mutable.addline(to: toPoint)
        drawingLayer.path = mutable
        drawingLayer.fillColor = nil
        drawingLayer.lineCap = kCALineCapRound
        drawingLayer.lineCap = 120 * CGFloat(lineWidthSliderView.value)
        //the bit in your code translated the whole thing to Int before translating it to a 
        //CGFloat, this is a bad idea since Ints cannot store decimals, so if there is no 
        //direct conversion, convert it to Double or Float
        drawingLayer.strokeColor = UIColor(calibratedRed: red/255, green: green/255, blue: blue/255 , alpha: 1).cgColor
    
    }
    

    你还需要做imageEditingView.addSubLayer(drawingLayer)

    另外,当您在context.move(to:) 和其他地方进行操作时,您不需要将 CGPoint 转换为另一个 CGPoint...

    【讨论】:

    • 非常感谢!这解决了问题,但是当我使用 UIImageWriteToSavedPhotosAlbum 时,它只保存了图像,它没有保存绘图层上的绘图。怎么保存上面有drawingLayer的图片??
    • 我找到了解决方案,将图像传递给另一个变量并保存它。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多