首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我使用了以下单点缩放图像的代码,但它不能保持图像的质量

我使用了以下单点缩放图像的代码,但它不能保持图像的质量
EN

Stack Overflow用户
提问于 2013-09-02 19:40:13
回答 1查看 1.3K关注 0票数 0
代码语言:javascript
复制
public static class ImageHelper
{
    public static UIImage Scale (UIImage source, SizeF newSize)
    {
        UIGraphics.BeginImageContext (newSize);
        var context = UIGraphics.GetCurrentContext ();
        context.InterpolationQuality=CGInterpolationQuality.High;
        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 Rotate (UIImage image)
    {

        UIImage res;

        using (CGImage imageRef = image.CGImage) {
            CGImageAlphaInfo alphaInfo = imageRef.AlphaInfo;
            CGColorSpace colorSpaceInfo = CGColorSpace.CreateDeviceRGB ();
            if (alphaInfo == CGImageAlphaInfo.None) {
                alphaInfo = CGImageAlphaInfo.NoneSkipLast;
            }

            int width, height;

            width = imageRef.Width;
            height = imageRef.Height;
            int maxSize = Math.Max (width, height);

            if (height >= width) {
                width = (int)Math.Floor ((double)width * ((double)maxSize / (double)height));
                height = maxSize;
            } else {
                height = (int)Math.Floor ((double)height * ((double)maxSize / (double)width));
                width = maxSize;
            }


            CGBitmapContext bitmap;

            if (image.Orientation == UIImageOrientation.Up || image.Orientation == UIImageOrientation.Down) {
                bitmap = new CGBitmapContext (IntPtr.Zero, width, height, imageRef.BitsPerComponent, imageRef.BytesPerRow, colorSpaceInfo, alphaInfo);
            } else {
                bitmap = new CGBitmapContext (IntPtr.Zero, height, width, imageRef.BitsPerComponent, imageRef.BytesPerRow, colorSpaceInfo, alphaInfo);
            }

            switch (image.Orientation) {
                case UIImageOrientation.Left:
                bitmap.RotateCTM ((float)Math.PI / 2);
                bitmap.TranslateCTM (0, -height);
                break;
                case UIImageOrientation.Right:
                bitmap.RotateCTM (-((float)Math.PI / 2));
                bitmap.TranslateCTM (-width, 0);
                break;
                case UIImageOrientation.Up:
                break;
                case UIImageOrientation.Down:
                bitmap.TranslateCTM (width, height);
                bitmap.RotateCTM (-(float)Math.PI);
                break;
            }

            bitmap.DrawImage (new Rectangle (0, 0, width, height), imageRef);


            res = UIImage.FromImage (bitmap.ToImage ());
            bitmap = null;

        }


        return res;
    }
}

调用method=> UIImage ScaledImage =ImageHelper.Scale (ImageHelper.Rotate (downloadedImage), new SizeF (270, 260));

请提供任何解决方案,以保持质量以及它将扩展到所需的大小。

EN

回答 1

Stack Overflow用户

发布于 2013-09-03 08:43:57

Xamarin的UIImage实现包含两个不同的Scale()方法

代码语言:javascript
复制
Scale(System.Drawing.SizeF) : UIImage
Scale(System.Drawing.SizeF, float) : UIImage
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18572421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档