ios - 在图像上绘制内容后,我的 ImageView contentmode 停止工作

标签 ios swift uiimage uigraphicscontext contentmode

感谢您的宝贵时间。

点击图片之前图片是好的,点击之后图片被压缩了。

这是我的代码:

我没有使用 Storyboard,所以我用代码创建所有内容,这里是 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()
}    

最佳答案

我不知道你所说的“压缩”是什么意思,但我猜图像以某种方式损坏了,因为当你将图像转换为 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...

关于ios - 在图像上绘制内容后,我的 ImageView contentmode 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40568849/

相关文章:

ios - 创建 MonoGame.Framework.iOS.dll

android - 带有搜索过滤器的Flutter应用 GridView ?

ios - 圆角不适用于除 iPhone XS max 和 XR 以外的所有模拟器

swift - 引用实例方法需要等效性 (SWIFT)

ios - 如何使用swift监控电池电量和状态变化

ios - 在 xib 中分配的图像是否已缓存?

ios - 带有 AFNetworking 的 UIImage

ios - 如何显示没有数据的 UIPageViewController?

ios - iPhone 4s-UIImage CGImage图像尺寸

ios - Facebook 登录后 Segue 不起作用