문제

디스크에서로드 된 이미지를 크기를 조정하려고합니다 - JPG 또는 PNG (로드 할 때 형식을 모릅니다) - 그런 다음 다시 디스크로 저장하십시오.

Objective-C에서 포트를 시도한 다음 코드가 있지만 마지막 부분에 갇혀 있습니다. 원래 대상 c.

이것은 내가하고 싶은 일을 달성하는 가장 좋은 방법이 아닐 수도 있습니다. 모든 해결책은 나에게 적합합니다.

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);
    */
}
도움이 되었습니까?

해결책

다가오는 Monotouch에서 우리는 스케일 방법을 가질 것입니다. 이것은 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;         
    }

Monotouch 외부에서 재사용하도록 조정 :

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

다른 팁

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

문맥에서 uiimage를 얻으려면 다음과 같은 일은 다음과 같습니다.

UIImage* result = UIImage.FromImage(context.ToImage());
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top