【问题标题】:How to fill a draw line with mosaic in an iOS app?如何在 iOS 应用程序中用马赛克填充画线?
【发布时间】:2015-12-06 17:37:37
【问题描述】:

我想在 swift 中构建一个 iOS 应用来做一些类似绘画应用的事情,但是我在我移动手指的路径上绘制并创建了马赛克。我有两个 imageView photoViewtempImageView。 PhotoView 加载照片和 tempImageView 进行绘制。这是一些代码

func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {

        UIGraphicsBeginImageContext(photoView.frame.size)
        let context = UIGraphicsGetCurrentContext()
        tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: photoView.frame.size.width, height: photoView.frame.size.height))

        CGContextMoveToPoint(context, fromPoint.x, fromPoint.y)
        CGContextAddLineToPoint(context, toPoint.x, toPoint.y)

        CGContextSetLineCap(context, CGLineCap.Round)
        CGContextSetLineWidth(context, brushWidth)
        //next line will fill the line with black color but how to fill with mosaic?
        CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0)
        CGContextSetBlendMode(context, CGBlendMode.Normal)

        CGContextStrokePath(context)

        tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }

我知道下一个代码可以为照片添加过滤器,但是如何将它放在路径或线条上?

var filter = CIFilter(name: "CIPixellate")
let inputImage = CIImage(image: originalImage)
filter.setValue(inputImage, forKey: kCIInputImageKey)

【问题讨论】:

标签: ios swift core-graphics core-image


【解决方案1】:

您可以在绘制代码中绘制蒙版图像,并使用蒙版合成像素化图像和原始图像。但为了获得良好的性能,您应该使用共享位图上下文。
如果您从位图上下文创建 CGImage,则 CGImage 创建时不会从上下文中复制数据。 CGBitmapContextCreateImage documentation.
稍后您可以使用CIBlendWithAlphaMaskCIBlendWithMask 过滤器来获取结果图像。

我认为这是一个好方法,但结果你会裁剪马赛克位。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-05
    • 2020-04-12
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    相关资源
    最近更新 更多