我想在swift中创建裁剪功能。它将显示一个巨大的图像视图,其中包含要裁剪的图像。然后在它的顶部另一个图像视图具有坚实的边界和透明背景,大约是巨型图像视图的1/3。
将允许用户拖动该叠加图像视图并将其分配到他们想要裁剪的巨型图像视图的一部分上。
然后,当单击裁剪按钮时,我只想抓取叠加图像视图所覆盖的底层巨型图像视图的部分。我不太清楚该怎么做。
这是我到目前为止所尝试的所有内容,它将巨型图像视图的顶部裁剪为覆盖图像视图的边界。但是我怎样才能改变它,以便它以正确的边界进行裁剪,但是使用巨大的图像视图的正确部分。现在它只是出于某种原因才裁掉它的顶部。
@IBOutlet weak var overlayImage: UIImageView!
@IBOutlet weak var imageView: UIImageView!
var lastLocation = CGPoint()
override func viewDidLoad() {
super.viewDidLoad()
//drawCustomImage simply creates a transparent background and dashed bordered box to display on the top 1/3 of the giant image view
let image = drawCustomImage()
overlayImage.image = image
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first{
self.lastLocation = touch.locationInView(self.view)
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first{
let location = touch.locationInView(self.view)
//don't let the user move the overlay image view passed the bounds of the giant image view
if(((location.y - self.lastLocation.y) + self.overlayImage.center.y) < (self.imageView.frame.maxY - (self.overlayImage.frame.height / 3)) && ((location.y - self.lastLocation.y) + self.overlayImage.center.y) > (self.imageView.frame.minY) + (self.overlayImage.frame.height / 3)){
self.overlayImage.center = CGPoint(x: self.overlayImage.center.x, y: (location.y - self.lastLocation.y) + self.overlayImage.center.y)
}
}
}
@IBAction func cropBtn(sender: AnyObject) {
let imageRef: CGImageRef = CGImageCreateWithImageInRect(imageView.image!.CGImage, overlayImage.bounds)!
imageView.bounds = overlayImage.bounds
imageView.image = UIImage(CGImage: imageRef)
overlayImage.hidden = true
}