簡體   English   中英

如何在iOS應用中使用馬賽克填充繪圖線?

[英]How to fill a draw line with mosaic in an iOS app?

我想快速構建一個iOS應用程序以執行繪畫應用程序之類的工作,但是我在移動手指的路徑上繪制並創建了馬賽克。 我有兩個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)

您可以在繪制代碼中繪制蒙版圖像,並可以使用蒙版合成像素化圖像和原始圖像。 但是為了獲得良好的性能,您應該使用共享位圖上下文。
如果從位圖上下文創建CGImage,則CGImage會創建沒有上下文的復制數據。 CGBitmapContextCreateImage文檔
稍后,您可以使用CIBlendWithAlphaMaskCIBlendWithMask過濾器來獲取結果圖像。

我認為這是個好方法,但是結果是您將裁剪掉馬賽克位。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM