简体   繁体   English

iOS Xamarin中的UIImage质量不好

[英]UIImage bad quality in iOS Xamarin

I have been using the code from here to add some text to my UIImage, however the quality on the image gets really bad. 我一直在这里使用代码向UIImage添加一些文本,但是图像的质量真的很差。 I have narrowed down the code a lot, to this: 我将代码范围缩小了很多:

    public UIImage EditImage(UIImage myImage)
    {
        using (CGBitmapContext ctx = new CGBitmapContext(IntPtr.Zero, 75, 75, 8, 4 * 75, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst))
        {
            ctx.DrawImage(new CGRect(0, 0, 75, 75), myImage.CGImage);
            return UIImage.FromImage(ctx.ToImage());
        }
    }

MyImage is a PNG image. MyImage是PNG图像。 I have added them like this: 我已经像这样添加了它们:

  • MyImage.png (100x100 pixels) MyImage.png(100x100像素)
  • MyImage@2.png (150x150 pixels) MyImage@2.png(150x150像素)
  • MyImage@3.png (200x200 pixels) MyImage@3.png(200x200像素)

I am not really sure which one of them is used, but when I inspect the image at runtime, the sizes are 75x75 nint (native int). 我不确定使用的是哪一种,但是在运行时检查图像时,大小为75x75 nint(本机int)。 That's why I set the CGBitmapContext height and width to 75. 这就是为什么我将CGBitmapContext的高度和宽度设置为75的原因。

My problem is that after processing myImage through this function the quality gets very poor. 我的问题是通过此函数处理myImage后,质量会变得很差。 If I skip this process and just use myImage the quality is excellent, however I need the CGBitmapContext to add some text to the image. 如果我跳过此过程而仅使用myImage,则质量非常好,但是我需要CGBitmapContext将一些文本添加到图像。

Does anyone have any idea what is causing it to be bad quality, and how I can fix it? 有谁知道导致质量下降的原因,以及如何解决? For the record, I am testing on an iPhone 6S. 作为记录,我正在iPhone 6S上进行测试。

You have multiple misunderstandings. 您有多种误解。

Lets start with the image sizes. 让我们从图像尺寸开始。 If your base image is 100x100 the @2x version should be 200x200 because it is double the resolution. 如果您的基本图像为100x100,则@ 2x版本应为200x200,因为它是分辨率的两倍。 Following this schema your @3x should be 300x300. 按照此架构,您的@ 3x应该为300x300。 The operating-system will choose the right one depending on the capabilities of your device. 操作系统将根据设备的功能选择合适的操作系统。 With a 6S it should be @2x depending on how you load the image. 对于6S,取决于您如何加载图像,它应该为@ 2x。

Lets move on to the way you are creating the CGBitmapContext. 让我们继续创建CGBitmapContext的方式。 The source of your code contains exactly what you need but you changed it to fix values and now wonder why your image is 75x75. 代码源完全包含您所需要的内容,但是您更改了它以固定值,现在想知道为什么图像为75x75。 The bad quality comes right from that, because the system is now resizing your original image. 糟糕的质量是由此而来的,因为系统现在正在调整原始图像的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM