HomeiOS Developmentc# - Attempting to Crop an Picture on iOS and a few...

c# – Attempting to Crop an Picture on iOS and a few Android units and returns with unsuitable place


personal IEnumerator CropImage(Texture authentic, int width, int peak)
{
    yield return new WaitForEndOfFrame();
    float originalWidth = authentic.width;
    float originalHeight = authentic.peak;
    float originalAspectRatio = originalWidth / originalHeight;
    float targetAspectRatio = (float)width / (float)peak;
    float scaleFactor;
    if (originalAspectRatio >= targetAspectRatio)
    {
        // Picture is wider than body. Use peak to find out scale.
        scaleFactor = peak / originalHeight;
        Debug.Log("[Image Editor]: Picture is wider than body. Scale Issue: " + scaleFactor);
    }
    else
    {
        // picture is taller than body. Use width to find out scale
        scaleFactor = width / originalWidth;
        Debug.Log("[Image Editor]: Picture is taller than body. Scale Issue: " + 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(authentic, rTex);

    Texture2D newTex = new Texture2D(width, peak, TextureFormat.RGB24, false);
    RenderTexture.energetic = rTex;

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

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

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

Howdy, I’ve this piece of code that crops a picture and works nicely within the editor and android, however for some cause in iOS and a few Android units it exhibits a unsuitable place. Anybody know why this occur? I noticed that RenderTexture have some bugs prior to now however I don’t know precisely what is occurring

I attempted completely different types of ReadPixels with the size I need however I do not discover something in the intervening time. I hope you’ll be able to assist me.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments