【问题标题】:How to Crop image in monotouch Xamarin如何在 monotouch Xamarin 中裁剪图像
【发布时间】:2014-07-26 13:28:15
【问题描述】:

我正在 UIImageview 中显示图像,我想裁剪图像,以下是我的要求。

选择裁剪图标应显示一个固定大小 (600X600) 的正方形,该正方形固定在图像上方,并带有网格线以帮助拉直图像。会有一个控件可以让图片在网格下翻转。

【问题讨论】:

    标签: c# xamarin.ios xamarin crop


    【解决方案1】:

    这就是我最终用来居中裁剪图像的方法。

    //Crops an image to even width and height
    public UIImage CenterCrop(UIImage originalImage)
    {
        // Use smallest side length as crop square length
        double squareLength = Math.Min(originalImage.Size.Width, originalImage.Size.Height);
    
        nfloat x, y;
        x = (nfloat)((originalImage.Size.Width - squareLength) / 2.0);
        y = (nfloat)((originalImage.Size.Height - squareLength) / 2.0);
    
        //This Rect defines the coordinates to be used for the crop
        CGRect croppedRect = CGRect.FromLTRB(x, y, x + (nfloat)squareLength, y + (nfloat)squareLength);
    
        // Center-Crop the image
        UIGraphics.BeginImageContextWithOptions(croppedRect.Size, false, originalImage.CurrentScale);
        originalImage.Draw(new CGPoint(-croppedRect.X, -croppedRect.Y));
        UIImage croppedImage = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();
    
        return croppedImage;
    }
    

    【讨论】:

      【解决方案2】:

      您可以尝试制作一个覆盖矩形来指定要裁剪的内容。提供 x 和 y(如果您的图像不在左上角。如果是,这些值都将为 0)、宽度和高度(对于您的示例,它们都应为 600)。我不知道允许网格线拉直图像的方法。我也不知道旋转图像的方法。但是对于直接图像,您可以使用如下所示的方法:

      private UIImage Crop(UIImage image, int x, int y, int width, int height)
      {
          SizeF imgSize = image.Size;
      
          UIGraphics.BeginImageContext(new SizeF(width, height));
          UIGraphics imgToCrop = UIGraphics.GetCurrentContext();
      
          RectangleF croppingRectangle = new RectangleF(0, 0, width, height);
          imgToCrop.ClipToRect(croppingRectangle);
      
          RectangleF drawRectangle = new RectangleF(-x, -y, imgSize.Width, imgSize.Height);
      
          image.Draw(drawRectangle);
          UIGraphics croppedImg = UIGraphics.GetImageFromCurrentImageContext();
      
          UIGraphics.EndImageContext();
          return croppedImg;
      } 
      

      【讨论】:

        猜你喜欢
        • 2022-01-02
        • 2012-05-08
        • 1970-01-01
        • 1970-01-01
        • 2015-06-24
        • 2015-02-02
        • 2012-11-16
        • 2021-07-24
        • 2021-06-24
        相关资源
        最近更新 更多