我想在swift中构建一个iOS应用程序来做一些类似绘画应用程序的事情,但我在我移动手指的路径上绘制和创建马赛克。我有两个imageView photoView和tempImageView。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)发布于 2015-12-09 20:15:43
你可以在你的绘图代码中绘制蒙版图像,并用蒙版合成像素化图像和原始图像。但是为了获得良好的性能,您应该使用共享位图上下文。
如果从位图上下文创建CGImage,则CGImage将在不从上下文复制数据的情况下创建。CGBitmapContextCreateImage documentation。
之后,您可以使用CIBlendWithAlphaMask或CIBlendWithMask过滤器来获取结果图像。
我认为这是一个很好的方法,但结果是你会有裁剪马赛克比特。
https://stackoverflow.com/questions/34120507
复制相似问题