Frage

Ich versuche, ein Bild von der Festplatte geladen, um die Größe - einem JPG oder PNG (ich weiß nicht, das Format, wenn ich es laden) - und dann auf der Festplatte speichern zurück .

Ich habe den folgenden Code bekam, die ich in den Hafen von Objective-C habe versucht, aber ich auf die letzten Teile stecken haben. Original-Objective-C .

Dies ist nicht der beste Weg zu erreichen sein kann, was ich tun will -. Jede Lösung ist gut für mich

int width = 100;
int height = 100;

using (UIImage image = UIImage.FromFile(filePath))
{
    CGImage cgimage = image.CGImage;
    CGImageAlphaInfo alphaInfo = cgimage.AlphaInfo;

    if (alphaInfo == CGImageAlphaInfo.None)
        alphaInfo = CGImageAlphaInfo.NoneSkipLast;

    CGBitmapContext context = new CGBitmapContext(IntPtr.Zero,
        width,
        height,
        cgimage.BitsPerComponent,
        4 * width,
        cgimage.ColorSpace,
        alphaInfo);

    context.DrawImage(new RectangleF(0, 0, width, height), cgimage);

    /*
    Not sure how to convert this part:

    CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
    UIImage*    result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);   // ok if NULL
    CGImageRelease(ref);
    */
}
War es hilfreich?

Lösung

In dem kommenden Monotouch werden wir eine Skala Verfahren haben, das ist seine Umsetzung in UIImage.cs:

    public UIImage Scale (SizeF newSize)
    {
        UIGraphics.BeginImageContext (newSize);
        var context = UIGraphics.GetCurrentContext ();
        context.TranslateCTM (0, newSize.Height);
        context.ScaleCTM (1f, -1f);

        context.DrawImage (new RectangleF (0, 0, newSize.Width, newSize.Height), CGImage);

        var scaledImage = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();

        return scaledImage;         
    }

Bereinigtes außerhalb von Monotouch wiederverwendet werden:

    public static UIImage Scale (UIImage source, SizeF newSize)
    {
        UIGraphics.BeginImageContext (newSize);
        var context = UIGraphics.GetCurrentContext ();
        context.TranslateCTM (0, newSize.Height);
        context.ScaleCTM (1f, -1f);

        context.DrawImage (new RectangleF (0, 0, newSize.Width, newSize.Height), source.CGImage);

        var scaledImage = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();

        return scaledImage;         
    }

Andere Tipps

    public static UIImage EditPhoto(int iMode, UIImage origImg)
    {
        SizeF newSize;

        if (iMode == 1 || iMode == 2)
            newSize = new SizeF(origImg.Size.Height, origImg.Size.Width);
        else
            newSize = origImg.Size;

        UIGraphics.BeginImageContext(newSize);
        CGContext ctx = UIGraphics.GetCurrentContext();

        switch (iMode)
        {
            case 1:         // Rotate counter-clockwise 90 degrees
                ctx.TranslateCTM(origImg.Size.Height, origImg.Size.Width);
                ctx.ScaleCTM(1f, -1f);
                ctx.RotateCTM(1.57079633f);     // angle is in radians
                break;

            case 2:         // Rotate clockwise 90 degrees
                ctx.ScaleCTM(1f, -1f);
                ctx.RotateCTM(-1.57079633f);     // angle is in radians
                break;

            case 3:         // Flip vertical
                // Do nothing. The image comes out flipped vertically because Core Graphics / OpenTK uses cartesian coordinates
                break;

            case 4:         // Flip horizontal
                ctx.TranslateCTM(newSize.Width, newSize.Height);
                ctx.ScaleCTM(-1f, -1f);
                break;

            default:        // Return unchanged image
                ctx.TranslateCTM(0, origImg.Size.Height);
                ctx.ScaleCTM(1f, -1f);
                break;
        }

        ctx.DrawImage(new RectangleF(0, 0, origImg.Size.Width, origImg.Size.Height), origImg.CGImage);
        UIImage resImg = UIGraphics.GetImageFromCurrentImageContext();

        UIGraphics.EndImageContext();
        return resImg;
    }

Um eine UIImage aus dem Kontext zu erhalten alles, was Sie tun müssen, ist:

UIImage* result = UIImage.FromImage(context.ToImage());
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top