裁剪旋转,平移和缩放图像

时间:2017-03-23 15:21:44

标签: ios swift uiscrollview uiimageview

我把UIImageView放在UIScrollView中。这个UIScrollView可以在垂直和水平方向滚动,可以缩放和旋转。 UIScrollView固定在Storyboard顶部/左/右/底部到superview,UIImageView固定在顶部/左边/ rigth /底部到UIScrollView。

在我设置的代码中:

override func viewDidLoad() {
    super.viewDidLoad()

    let rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(didRotateSelectedPhoto(_:)))
    rotationGesture.delegate = self
    selectedPhotoScrollView.addGestureRecognizer(rotationGesture)
}

func didRotateSelectedPhoto(_ sender: UIRotationGestureRecognizer) {
    if sender.state == .began {
        lastRotation = 0.0
    }
    else {
        let rotation = 0.0 - (lastRotation - sender.rotation)
        selectedPhotoScrollView.transform = selectedPhotoScrollView.transform.rotated(by: rotation)
        lastRotation = sender.rotation
    }
}

fileprivate func updateSelectedPhotoView(withPhoto photo: UIImage) {
    selectedPhotoImageView.image = photo

    let minZoomScale: CGFloat
    if cropPortraitContainerView.isHidden {
        minZoomScale = photosContainerView.frame.width / photo.size.width
        selectedPhotoScrollView.contentInset = UIEdgeInsetsMake(landscapeCropHeightCostraint.constant, 0.0, landscapeCropHeightCostraint.constant, 0.0)
    }
    else {
        minZoomScale = photosContainerView.frame.height / photo.size.height
        selectedPhotoScrollView.contentInset = UIEdgeInsetsMake(0.0, portraitCropWidthConstraint.constant, 0.0, portraitCropWidthConstraint.constant)
    }

    selectedPhotoScrollView.minimumZoomScale = minZoomScale
    selectedPhotoScrollView.maximumZoomScale = 1.2
    selectedPhotoScrollView.setZoomScale(minZoomScale, animated: false)
}

extension PhotoPickerViewController: UIScrollViewDelegate {

    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return selectedPhotoImageView
    }
}

extension PhotoPickerViewController: UIGestureRecognizerDelegate {

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

平移,缩放旋转工作完美。我的目标是裁剪此图片。

以下是我的观点:

enter image description here

我可以非常轻松地裁剪平移和缩放图像:

let originX: CGFloat
let originY: CGFloat
let width: CGFloat
let height: CGFloat
if cropPortraitContainerView.isHidden {
    originX = selectedPhotoScrollView.contentOffset.x / selectedPhotoScrollView.zoomScale
    originY = (selectedPhotoScrollView.contentOffset.y + landscapeCropHeightCostraint.constant) / selectedPhotoScrollView.zoomScale
    width = photosContainerView.frame.width / selectedPhotoScrollView.zoomScale
    height = (photosContainerView.frame.height - 2 * landscapeCropHeightCostraint.constant) / selectedPhotoScrollView.zoomScale
}
else {
    originX = (selectedPhotoScrollView.contentOffset.x + portraitCropWidthConstraint.constant) / selectedPhotoScrollView.zoomScale
    originY = selectedPhotoScrollView.contentOffset.y / selectedPhotoScrollView.zoomScale
    width = (selectedPhotoScrollView.frame.width - 2 * portraitCropWidthConstraint.constant) / selectedPhotoScrollView.zoomScale
    height = selectedPhotoScrollView.frame.height / selectedPhotoScrollView.zoomScale
}

let rotatedPoint = CGPoint(x: originX, y: originY).applying(CGAffineTransform(rotationAngle: lastRotation))

if let croppedImage = image.cropedToRect(rect: CGRect(x: rotatedPoint.x, y: rotatedPoint.y, width: width, height: height)) {
    delegate?.photoPicker(photoPicker: self, didSelectPhoto: croppedImage)
}

旋转图像时问题开始。有人可以给我看一个示例代码吗?

0 个答案:

没有答案