我正在 UIImageview 中显示图像,并且我想裁剪图像,以下是我的要求。
选择裁剪图标应显示一个固定大小 (600X600) 的正方形,该正方形固定在带有网格线的图像上,以帮助拉直图像。网格下会有一个允许图像翻转的控件。
最佳答案
这就是我最终用来居中裁剪图像的方法。
//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;
}
关于c# - 如何在 monotouch Xamarin 中裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24971499/