我正在尝试将两种机制结合起来,而且我在找到解决方案方面并没有走得太远。这是我希望能够做到的: -
我有一个例程,允许我在UIImage图层上绘制一个蒙版,下面有一个第二个UIImage,其中包含我在上面绘制蒙版的图像。这似乎工作得很好,当我精简时,我只需要组合2层然后应用蒙版,图像看起来像是“切出”。
问题是我希望能够缩放,平移遮罩和图像,以便能够在缩放模式下应用更准确的遮罩。该应用程序的一部分使用GestureRecognizers来移动图像以获取屏幕截图,以为我可以将蒙版绘图与Gesture例程相结合。这是我迷路的地方,但不起作用。
面具绘图程序
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
swiped = false
if let touch = touches.first as? UITouch {
lastPoint = touch.locationInView(self.view)
}
}
func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {
UIGraphicsBeginImageContext(tempImageView.frame.size)
let context = UIGraphicsGetCurrentContext()
tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y)
CGContextAddLineToPoint(context, toPoint.x, toPoint.y)
CGContextSetLineCap(context, kCGLineCapRound)
CGContextSetLineWidth(context, brushWidth)
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 1.0, opacity)
if maskSwitch == 1 {
CGContextSetBlendMode(context, kCGBlendModeClear)
} else {
CGContextSetBlendMode(context, kCGBlendModeNormal)
}
CGContextStrokePath(context)
tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
tempImageView.alpha = opacity
UIGraphicsEndImageContext()
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
swiped = true
if let touch = touches.first as? UITouch {
let currentPoint = touch.locationInView(self.view)
drawLineFrom(lastPoint, toPoint: currentPoint)
lastPoint = currentPoint
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
if !swiped {
// draw a single point
drawLineFrom(lastPoint, toPoint: lastPoint)
}
}
手势识别器例程
@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translationInView(self.view)
recognizer.view!.center = CGPoint(x:recognizer.view!.center.x + translation.x,
y:recognizer.view!.center.y + translation.y)
recognizer.setTranslation(CGPointZero, inView: self.view)
}
func gestureRecognizer(UIGestureRecognizer,
shouldRecognizeSimultaneouslyWithGestureRecognizer:UIGestureRecognizer) -> Bool {
return true
}
@IBAction func handlePinch(recognizer : UIPinchGestureRecognizer) {
recognizer.view!.transform = CGAffineTransformScale(recognizer.view!.transform,
recognizer.scale, recognizer.scale)
recognizer.scale = 1
}
@IBAction func handleRotate(recognizer : UIRotationGestureRecognizer) {
recognizer.view!.transform = CGAffineTransformRotate(recognizer.view!.transform, recognizer.rotation)
recognizer.rotation = 0
}
不确定这是否相关,但这是应用蒙版的例程
func maskImage(image:UIImage,maskImage:UIImage)-> UIImage{
var maskRef:CGImageRef = maskImage.CGImage
var mask:CGImageRef = CGImageMaskCreate(CGImageGetWidth(maskRef), CGImageGetHeight(maskRef), CGImageGetBitsPerComponent(maskRef), CGImageGetBitsPerPixel(maskRef), CGImageGetBytesPerRow(maskRef), CGImageGetDataProvider(maskRef), nil, true)
var masked:CGImageRef = CGImageCreateWithMask(image.CGImage, mask)
return UIImage(CGImage: masked)!
}
对此的任何帮助都会很棒。
谢谢, 安东尼