debugging 尝试在iOS和一些Android设备上裁剪图像,并返回错误的位置

yduiuuwa  于 2023-08-06  发布在  iOS
关注(0)|答案(1)|浏览(68)
private IEnumerator CropImage(Texture original, int width, int height)
{
    yield return new WaitForEndOfFrame();
    float originalWidth = original.width;
    float originalHeight = original.height;
    float originalAspectRatio = originalWidth / originalHeight;
    float targetAspectRatio = (float)width / (float)height;
    float scaleFactor;
    if (originalAspectRatio >= targetAspectRatio)
    {
        // Image is wider than frame. Use height to determine scale.
        scaleFactor = height / originalHeight;
        Debug.Log("[Image Editor]: Image is wider than frame. Scale Factor: " + scaleFactor);
    }
    else
    {
        // image is taller than frame. Use width to determine scale
        scaleFactor = width / originalWidth;
        Debug.Log("[Image Editor]: Image is taller than frame. Scale Factor: " + scaleFactor);
    }

    int newWidth = Mathf.FloorToInt(originalWidth * scaleFactor * _zoomScale);
    int newHeight = Mathf.FloorToInt(originalHeight * scaleFactor * _zoomScale);

    RenderTexture rTex = new RenderTexture(newWidth, newHeight, 24, RenderTextureFormat.ARGB32);
    Graphics.Blit(original, rTex);

    Texture2D newTex = new Texture2D(width, height, TextureFormat.RGB24, false);
    RenderTexture.active = rTex;

    Vector2 imagePositions = m_Image.rectTransform.localPosition;
    float xPos = (newWidth - width) * 0.5f - imagePositions.x;
    float yPos = (newHeight - height) * 0.5f + imagePositions.y;

    newTex.ReadPixels(new Rect(new Vector2(xPos, yPos), m_Image.rectTransform.rect.size), 0, 0);
    //newTex.ReadPixels(new Rect(xPos, yPos, ImageContainer.rect.width, ImageContainer.rect.height), 0, 0);
    yield return new WaitForEndOfFrame();

    newTex.Apply();
    finalTex = newTex;
    m_CroppedImage.texture = finalTex;
    _data.OnSetImage?.Invoke(finalTex);
    UIManager.Inst.RemoveTopModal();
}

字符串
你好,我有这段代码,作物的图像,并在编辑器和Android工作良好,但由于某种原因,在iOS和一些Android设备,它显示了一个错误的位置.有人知道为什么会这样吗?我看到RenderTexture在过去有一些bug,但我不知道到底发生了什么
我尝试了不同形式的ReadPixels与我想要的尺寸,但我没有找到任何的时刻。我希望你能帮我。

qij5mzcb

qij5mzcb1#

解决了。我已经发现了这个问题,当我说提供图形API时,我是对的。感谢Bunny帮助我认识到这一点。我必须验证我使用的图形API
如果你有更干净的东西来验证这一点,请与我分享。

float xPos = ((newWidth - width) * 0.5f) - ImageContainer.anchoredPosition.x;
    float yPos = ((newHeight - height) * 0.5f) + ImageContainer.anchoredPosition.y;

    if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.OpenGLCore || SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.OpenGLES2 || SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.OpenGLES3 ||SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Metal)
    {
        yPos = ((newHeight - height) * 0.5f) + (1 - ImageContainer.anchoredPosition.y);
    }

字符串

相关问题