Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS not Loading picture #43

Closed
andersonaraujodl opened this issue Aug 3, 2018 · 5 comments
Closed

iOS not Loading picture #43

andersonaraujodl opened this issue Aug 3, 2018 · 5 comments

Comments

@andersonaraujodl
Copy link

Hello!

First I'd like to thank you for the incredible work!

On android it works perfectly although I'm facing an issue on iOS. The first time I call my gallery method it asks for permission, opens the gallery, but does nothing after that. It looks like it doesn't catch the path on the first attempt, but on my second try, it doesn't matter which picture I choose, it brings the previous picture instead. Something like if it holds the picture somewhere, but never uses it, and brings it back later, on the next call.

I'm using it together with the ImageCrop made yourself, but not sure if I did something wrong! :(
Would be able to help me on this?

Thanks in advance.

@yasirkula
Copy link
Owner

Hi! It seems as if NativeGallery returns the image's path before it is written to the disk/while it is being written to the disk, but AFAIK this line ensures that image is written to the disk synchronously: https://github.com/yasirkula/UnityNativeGallery/blob/master/Plugins/NativeGallery/iOS/NativeGallery.mm#L482

Can you see any error messages in Xcode console? If you use the example code to display the selected image on a cube without using the ImageCropper plugin, does everything work properly?

@andersonaraujodl
Copy link
Author

Thanks for you reply!
I'll try to make the suggested modification without the ImageCropper and will get back to you ASAP!

@andersonaraujodl
Copy link
Author

Well, I've got a feedback: did all the tests and realized that there is something wrong with my logic (just couldn't figure out why it works perfect on Android and not on iOS). I'm attaching the sample bellow to see if there's something you can help with.

private IEnumerator CropPic()
    {
        yield return new WaitForEndOfFrame();

        bool ovalSelection = false;
        bool autoZoom = true;

        float minAspectRatio = 1, maxAspectRatio = 1;

        NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
        {
            Debug.Log("Image path: " + path);
            if (path != null)
            {
                // Create Texture from selected image
                texture = NativeGallery.LoadImageAtPath(path, 1024);

                if (texture == null)
                {
                    Debug.Log("Couldn't load texture from " + path);
                    return;
                }

                if (ImageCropper.Instance.IsOpen)
                    return;
            }
        }, LanguageManager.GetTranslation("SelectImage"), "image/png", 1024);

        newProfileInstance.previousTexture = (Texture2D) newProfileInstance.profileImage.texture;

        // If image cropper is already open, do nothing
        if (ImageCropper.Instance.IsOpen)
            ImageCropper.Instance.Hide();

        ImageCropper.Instance.Show(texture, (bool result, Texture originalImage, Texture2D croppedImage) =>
        {
            if (result)
            {
                newProfileInstance.profileImage.texture = croppedImage;

                if (croppedImage != null)
                {
                    newProfileInstance.UploadImage(ConvertImageToBytes(MakeTextureReadable(croppedImage)), croppedImage);
                    croppedTexture = croppedImage;
                }
            }
            else
            {
                croppedTexture = null;
            }
        },
        settings: new ImageCropper.Settings()
        {
            ovalSelection = ovalSelection,
            autoZoomEnabled = autoZoom,
            imageBackground = Color.clear, // transparent background
                selectionMinAspectRatio = minAspectRatio,
            selectionMaxAspectRatio = maxAspectRatio

        },
        croppedImageResizePolicy: (ref int width, ref int height) =>
        {
                //width /= 2;
                //height /= 2;
        });
    }

@yasirkula
Copy link
Owner

Oh, I see. You should just move ImageCropper.Instance.Show (in other words, all the code) into the callback function of NativeGallery.GetImageFromGallery, because the callback function is asynchronous on iOS and in your case, the assignation of the texture variable takes place after the ImageCropper.Instance.Show call.

@andersonaraujodl
Copy link
Author

It worked perfectly! I appreciate your work and your help! Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants