我在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;
}