图像裁剪在裁剪期间抓住UIImage的错误部分

时间:2016-09-08 02:20:05

标签: swift core-graphics crop

我一直致力于制作一个视图控制器,它会将图像缩小到特定大小,并带有一些可拖动的控制点,并且裁剪区域外的背景图像会变暗。

由于某些原因,无论何时裁剪图像,它都会抓取错​​误的参考。我已经看过几乎所有关于这个问题的帖子来处理裁剪。

这是我对故事板的设置: enter image description here

我已经问过其他一些人,包括我所参加的课程的导师和导师,但我们似乎都很难过。

我可以通过在视图控制器周围拖动UL UR DL DR角来选择一个帧,如下所示:

Notice the files selected from the right of the screen.

但是当我按下按钮并使用我写过的裁剪功能时,根据框架选择,我得到的东西不是正确的裁剪。

enter image description here

我在裁剪过程中也收到此错误消息:

 2016-09-07 23:36:38.962 ImageCropView[33133:1056024] 
  <UIView: 0x7f9cfa42c730; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x7f9cfa408400>>'s window
  is not equal to <ImageCropView.CroppedImageViewController: 0x7f9cfa43f9b0>'s view's window!

代码的违规部分必须位于以下某个功能中。

这是裁剪功能:

 func cropImage(image: UIImage, toRect rect: CGRect) -> UIImage {


        func rad(deg: CGFloat) -> CGFloat {
            return deg / 180.0 * CGFloat(M_PI)
        }
        // determine the orientation of the image and apply a transformation to the crop rectangle to shift it to the correct position
        var rectTransform: CGAffineTransform
        switch image.imageOrientation {
        case .Left:
            rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(90)), 0, -image.size.height)
        case .Right:
            rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-90)), -image.size.width, 0)
        case .Down:
            rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-180)), -image.size.width, -image.size.height)
        default:
            rectTransform = CGAffineTransformIdentity
        }




        // adjust the transformation scale based on the image scale
        rectTransform = CGAffineTransformScale(rectTransform, UIScreen.mainScreen().scale, UIScreen.mainScreen().scale)
        // apply the transformation to the rect to create a new, shifted rect
        let transformedCropSquare = CGRectApplyAffineTransform(rect, rectTransform)
        // use the rect to crop the image
        let imageRef = CGImageCreateWithImageInRect(image.CGImage, transformedCropSquare)
        // create a new UIImage and set the scale and orientation appropriately
        let result = UIImage(CGImage: imageRef!, scale: image.scale, orientation: image.imageOrientation)

        return result
    }

以下是设置和翻译蒙版视图的功能

    func setTopMask(){
        let path = CGPathCreateWithRect(cropViewMask.frame, nil)
        topMaskLayer.path = path
        topImageView.layer.mask = topMaskLayer

    }

    func translateMask(sender: UIPanGestureRecognizer) {
        let translation = sender.translationInView(self.view)
        sender.view!.center = CGPointMake(sender.view!.center.x + translation.x, sender.view!.center.y + translation.y)
        //        print(sender.translationInView(self.view))
        sender.setTranslation(CGPointZero, inView: self.view)
        //        print("panned mask")

        if sender.state == .Ended {
            printFrames()
        }

    }


  func setCropMaskFrame() {
        let x = ulCorner.center.x
        let y = ulCorner.center.y
        let width = urCorner.center.x - ulCorner.center.x
        let height = blCorner.center.y - ulCorner.center.y

        cropViewMask.frame = CGRectMake(x, y, width, height)
        setTopMask()
    }

1 个答案:

答案 0 :(得分:2)

我知道这是很久以前的...只是一个想法,我遇到了类似的问题,我发现裁剪的框架很可能是正确的。问题在于您尝试裁剪的图片的实际尺寸。我通过将保存图片的视图大小与实际图片大小(以磅为单位)对齐来解决问题。然后裁剪区域裁剪了所选择的内容。我知道这可能不是一个解决方案,只是分享我的经验,希望它有助于打开一些灯泡:)