当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - MonoTouch, drawing a cropping region layer over a UIImageView

问题描述:

In our app we'd like to be able to crop, scale and pan an image, and I just can't seem to figure out how I am supposed to draw a cropping region on top of my UIImageView.

I tried messing with coregraphics, I could render a region with a black stroke on my image, but the image would flip. Not only that, but since I had drawn ON the image, I'm afraid that if I were to move and scale it using gestures, the region would be affected too!

A push in the right direction would be much appreciated!

Heres my code that doesen't really do what I want, to show some research effort.

 // 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();

网友答案:

Maybe this will help you:

    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;
    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: