【问题标题】:Crop image according to frame of any View根据任何视图的框架裁剪图像
【发布时间】:2019-02-23 10:21:20
【问题描述】:

我有一张图片,我想根据任何视图的框架对其进行裁剪。例如;

我真的找不到解决方案。我已经搜索了 2 天。

BEFORE

AFTER

已编辑

感谢@Ajharul Islam 和@Bence Pattogato。两个答案都有效。

@Ajharul Islam 解决方案的 Swift 版本。

      func images(byCroppingImage image: UIImage?, to size: CGSize) -> UIImage? {
        // not equivalent to image.size (which depends on the imageOrientation)!
        let refWidth = (image?.cgImage?.width)!
        let refHeight = (image?.cgImage?.height)!


        let x = (Double(refWidth) - Double(size.width)) / 2
        let y = (Double(refHeight) - Double(size.height)) / 2



        let cropRect = CGRect(x: CGFloat(x), y: CGFloat(y), width: size.width, height: size.height)


        let imageRef = image?.cgImage!.cropping(to: cropRect) as! CGImage

        var cropped: UIImage? = nil
        if let imageRefs = image?.cgImage!.cropping(to: cropRect) {
            cropped = UIImage(cgImage: imageRefs, scale: 0.0, orientation: UIImage.Orientation.up)
        }


        return cropped
    }

让我告诉我我的错误以及我在尝试什么

我正在尝试拍照并根据任何视图的框架对其进行裁剪。我试图在不根据该主视图调整大小的情况下裁剪图片。所以每次都是从错误的方式裁剪。

我调整了图片的大小,现在我可以成功裁剪图片了。但是调整大小会降低图片的质量。所以现在我正在努力寻找最好的方法。

谢谢

【问题讨论】:

  • 您需要导出图像还是仅用于查看目的??
  • 我实际上需要将裁剪后的图像存储在变量中。所以我可以重复使用它。

标签: ios image crop


【解决方案1】:

我认为最简单的实现方法如下:

extension UIImage {
    func crop(to rect: CGRect) -> UIImage? {
        guard let imageRef = cgImage, let cropped = imageRef.cropping(to: rect) else {
            return nil
        }
        return UIImage(cgImage: cropped)
    }
}

【讨论】:

    【解决方案2】:

    您可以使用此代码裁剪图像:

       - (UIImage *)imageByCroppingImage:(UIImage *)image toSize:(CGSize)size
    {
        // not equivalent to image.size (which depends on the imageOrientation)!
        double refWidth = CGImageGetWidth(image.CGImage);
        double refHeight = CGImageGetHeight(image.CGImage);
    
        double x = (refWidth - size.width) / 2.0;
        double y = (refHeight - size.height) / 2.0;
    
        CGRect cropRect = CGRectMake(x, y, size.height, size.width);
        CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
    
        UIImage *cropped = [UIImage imageWithCGImage:imageRef scale:0.0 orientation:self.imageOrientation];
        CGImageRelease(imageRef);
    
        return cropped;
    }
    

    传递您的图像和视图的矩形,您将从中心获得裁剪图像

    【讨论】:

      【解决方案3】:

      您正在搜索 CIAffineTransformCIPerspectiveCorrection。我不确定哪个更适合您的用例,但它们应该都可以工作。例如,您可以像这样使用 CIPerspectiveCorrection:

      (CIImageToWorkWith).applyingFilter("CIPerspectiveCorrection", parameters: [
                                  "inputTopLeft" : CIVector(cgPoint: topleft),
                                  "inputTopRight" : CIVector(cgPoint: topright),
                                  "inputBottomLeft" : CIVector(cgPoint: bottomleft),
                                  "inputBottomRight" : CIVector(cgPoint: bottomright)
                                  ])
      

      edit:您不想裁剪图像。裁剪就像从图像中剪下一些东西而不对其进行缩放。

      【讨论】:

        猜你喜欢
        • 2016-05-04
        • 1970-01-01
        • 1970-01-01
        • 2013-01-14
        • 2013-12-30
        • 2016-05-04
        • 1970-01-01
        • 1970-01-01
        • 2012-11-02
        相关资源
        最近更新 更多