Resize and saving an image to disk in Monotouch

后端 未结 3 486
悲&欢浪女
悲&欢浪女 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: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;         
        }
    

提交回复
热议问题