gpt4 book ai didi

ios - MonoTouch,在 UIImageView 上绘制裁剪区域层

转载 作者:行者123 更新时间:2023-12-01 19:15:29 24 4
gpt4 key购买 nike

在我们的应用程序中,我们希望能够裁剪、缩放和平移图像,但我似乎无法弄清楚应该如何在 UIImageView 上绘制裁剪区域。

我试着弄乱 coregraphics,我可以在我的图像上渲染一个带有黑色笔划的区域,但图像会翻转。不仅如此,因为我已经在图像上绘制了,我担心如果我用手势移动和缩放它,该区域也会受到影响!

朝着正确的方向插入将不胜感激!

这是我的代码并没有真正做我想要的,以展示一些研究工作。

        // Aspect ration - Currently 1:1
const int arWidth = 1;
const int arHeight = 1;

UIGraphics.BeginImageContext(ImgToCrop.Frame.Size);

var context = UIGraphics.GetCurrentContext();

// Set the line width
context.SetLineWidth(4);
UIColor.Black.SetStroke();

// Our starting points.
float x = 0, y = 0;

// The sizes
float width = ImgToCrop.Frame.Width, height = ImgToCrop.Frame.Height;

// Calculate the geometry
if(arWidth == arHeight){
// The aspect ration is 1:1
width = ImgToCrop.Frame.Width;
height = width;

x = 0;
y = ImgToCrop.Frame.GetMidY()-height/2;

}

// The rect
var drawRect = new RectangleF(x,y,width,height);

context.DrawImage(new RectangleF(
ImgToCrop.Frame.X,
ImgToCrop.Frame.Y,
ImgToCrop.Frame.Width,
ImgToCrop.Frame.Height),ImgToCrop.Image.CGImage);

// Draw it
context.StrokeRect(drawRect);
ImgToCrop.Image = UIGraphics.GetImageFromCurrentImageContext();

最佳答案

也许这会帮助你:

    public static UIImage ScaleToSize (UIImage image, int width, int height)
{
UIGraphics.BeginImageContext (new SizeF (width, height));
CGContext ctx = UIGraphics.GetCurrentContext ();
float ratio = (float) width / (float) height;

ctx.AddRect (new RectangleF (0.0f, 0.0f, width, height));
ctx.Clip ();

var cg = image.CGImage;
float h = cg.Height;
float w = cg.Width;
float ar = w / h;

if (ar != ratio) {
// Image's aspect ratio is wrong so we'll need to crop
float scaleY = height / h;
float scaleX = width / w;
PointF offset;
SizeF crop;
float size;

if (scaleX >= scaleY) {
size = h * (w / width);
offset = new PointF (0.0f, h / 2.0f - size / 2.0f);
crop = new SizeF (w, size);
} else {
size = w * (h / height);
offset = new PointF (w / 2.0f - size / 2.0f, 0.0f);
crop = new SizeF (size, h);
}

// Crop the image and flip it to the correct orientation (otherwise it will be upside down)
ctx.ScaleCTM (1.0f, -1.0f);
using (var copy = cg.WithImageInRect (new RectangleF (offset, crop))) {
ctx.DrawImage (new RectangleF (0.0f, 0.0f, width, -height), copy);
}
} else {
image.Draw (new RectangleF (0.0f, 0.0f, width, height));
}

UIImage scaled = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();

return scaled;
}

关于ios - MonoTouch,在 UIImageView 上绘制裁剪区域层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13742037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com