Resize and saving an image to disk in Monotouch

后端 未结 3 470
悲&欢浪女
悲&欢浪女 2020-12-17 00:10

I\'m trying to resize an image loaded from disk - a JPG or PNG (I don\'t know the format when I load it) - and then save it back to disk.

I\'ve got the fol

相关标签:
3条回答
  • 2020-12-17 00:35
        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;
        }
    
    0 讨论(0)
  • 2020-12-17 00:36

    In the upcoming MonoTouch we will have a scale method, this is its implementation 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;         
        }
    

    Adjusted to be reused outside of 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;         
        }
    
    0 讨论(0)
  • 2020-12-17 00:47

    To get an UIImage from context all you need to do is:

    UIImage* result = UIImage.FromImage(context.ToImage());
    
    0 讨论(0)
提交回复
热议问题