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

ImagePicker returning same image #306

Merged
merged 6 commits into from
Sep 16, 2019

Conversation

klodha
Copy link
Contributor

@klodha klodha commented Jan 5, 2018

iOS 10.2+ web view keep returning same image regardless whichever we pick from gallery view, given if we use FILE_URI method. I was looking for a solution of this issue, and while most suggestion was to use DATA_URI instead of FILE_URI that increases memory usage (at least in my case). After putting some breakpoint and debugging here is what I found:

  • Cordova app code request camera to pick a image from gallery with FILE_URI destination
  • CDVCamera.m contains code which execute that native task and store file at a temporary location and return its URI.
  • In order to get the temporary file name, tempFilePath method is used, which sets an integer value of 1 and then loop through temporary directory until it find an unused name with incrementing that value.
  • Cordova app consume the URI and read data.
  • As a best practice it calls cleanup() for freeing temporary resource.
  • CDVCamera.m removes the temporary file.

This all flow works fine. However next time in same session when client request another picture, tempFilePath method returns same filename because there is no previous file holding the name. That is also fine, but here the twist by web view, which getting same URL returns old image (from browser's cache). Experimenting with filename gives a fix that if we use a random filename, it works as expected.

So I propose this change (which should work almost in all cases, as picking a image from gallery will take in general more than 1 second), which uses unix timestamp value instead of integer i.

I don't have coding experience with Objective-C, so code change between line #402-404 can be updated to use better approach instead of 3 objects (date, interval and number).

Platforms affected

What does this PR do?

What testing has been done on this change?

Checklist

  • Reported an issue in the JIRA database
  • Commit message follows the format: "CB-3232: (android) Fix bug with resolving file paths", where CB-xxxx is the JIRA ID & "android" is the platform affected.
  • Added automated test coverage as appropriate for this change.

iOS 10.2+ web view keep returning same image regardless whichever we pick from gallery view, given if we use FILE_URI method. I was looking for a solution of this issue, and while most suggestion was to use DATA_URI instead of FILE_URI that increases memory usage (at least in my case). After putting some breakpoint and debugging here is what I found: 

- Cordova app code request camera to pick a image from gallery with FILE_URI destination
- CDVCamera.m contains code which execute that native task and store file at a `temporary` location and return its URI. 
- In order to get the temporary file name, `tempFilePath` method is used, which sets an integer value of 1 and then loop through temporary directory until it find an unused name with incrementing that value. 
- Cordova app consume the URI and read data. 
- As a best practice it calls `cleanup()` for freeing temporary resource. 
- CDVCamera.m removes the temporary file. 

This all flow works fine. However next time in same session when client request another picture, tempFilePath method returns same filename because there is no previous file holding the name. That is also fine, but here the twist by web view, which getting same URL returns old image (from browser's cache). Experimenting with filename gives a fix that if we use a random filename, it works as expected. 

So I propose this change (which should work almost in all cases, as picking a image from gallery will take in general more than 1 second), which uses unix timestamp value instead of  integer ` i`. 

I don't have coding experience with Objective-C, so code change between line apache#402-404 can be updated to use better approach instead of 3 objects (date, interval and number).
@D-Marc1
Copy link

D-Marc1 commented Sep 1, 2018

@janpio Any particular reason why this hasn't been merged yet? This plugin is impossible to use in production, unless you use the more memory-intensive DATA_URL. With FILE_URI, it'll use previously selected images that are cached, even when a new one is selected.

@janpio
Copy link
Member

janpio commented Sep 1, 2018

Any particular reason why you mention someone (me) in a PR who hasn't appeared in its reviews, description or comments? I consider this pretty impolite. I am not your free labor - even when I am active in other issues or pull requests.

I will answer you question anyway:

Any particular reason why this hasn't been merged yet?

No. Just because nobody reviewed and merged this PR until now. Apache Cordova is a bunch of volunteers, so stuff happens when it happens.

You can help by checking out @klodha's branch of the plugin yourself and test it, confirm that it works and leave an "Approve" review or whatever you experience when testing as a comment. If there are indicators a PR is good, it is much easier for a Cordova contributor to jump in, which of course moves the PR to the top of the pile.

src/ios/CDVCamera.m Outdated Show resolved Hide resolved
@D-Marc1
Copy link

D-Marc1 commented Sep 1, 2018

My apologies, my intent was not to be rude. I just wasn't sure how to get this 8 month old PR the attention it deserves. My fear was that this would never end up being merged, even though this plugin is unusable as is for a live production app (with FILE_URI). I've been using @klodha's branch for a few months, and it works like a charm. Figured it would be good idea to get this plugin production-ready.

Copy link
Member

@janpio janpio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.
Please another review before merge.

@klodha
Copy link
Contributor Author

klodha commented Sep 7, 2018

Hi,

Only change I made in plugin was iOS source files. However I see that Travis CI is failing build. Further checking the details, I found its failing build for Browser platform. So that is not related to the change I did. Is there anything which I can do to get Travis test getting passed as well?

Thanks

@janpio
Copy link
Member

janpio commented Sep 7, 2018

The same test failed on the last pure docs commit to master, so this is definitely not something caused by your PR.

Still, someone else besides me should review and approve this PR before we merge it. I just don't know enough to merge this in myself.

@janpio janpio added this to ⛔ Blocked: Tests failing in Apache Cordova: Plugin Pull Requests Sep 16, 2018
@janpio janpio moved this from ⛔ Blocked: Tests failing to ⏳ Ready for Review in Apache Cordova: Plugin Pull Requests Sep 30, 2018
@klodha
Copy link
Contributor Author

klodha commented Dec 13, 2018

Hi,

Just checking if anyone got chance to review this change :) For another project I started using camera plugin and then it reminded me on this.

No hurries, just wanted to ensure if its in review queue still :)

@janpio
Copy link
Member

janpio commented Dec 13, 2018

It is definitely in the queue, but the queue is pretty endless and not many people are working on it right now. If you can, please test out this PR in your own app and let us know if it works by reviewing the PR in the "Files changed" tab. Thanks.

@klodha
Copy link
Contributor Author

klodha commented Dec 19, 2018

Hi. I can confirm that this works. I have an application in App Store which is using this change. And since then tried this in 2 other apps including the most recent one, and this fix seems to be working fine for me.

Mainly the change I made in this PR is generating unique file name for temporary file using the timestamps. That way web view can't cache it for each new image. With the original approach, it was using an integer starting from 1, and in each session it was creating same file name as last one. Web views got confused perhaps and thought we are requesting same file and started returning from the cache. That was the origin and alternate was making each url unique, and using a timestamp sound like a good option to me. As any camera action would take enough time to ensure that we get different value.

Hope this helps.

Copy link
Contributor Author

@klodha klodha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems to be working fine in new project as well existing project in which I used it. Whole point of this change to generate a unique file name, for which timestamp seems best candidate. Because gap between 2 consecutive camera operation allows enough time to make a difference in this value. So instead of an integer starting with 1 for each camera session, started using timestamp to ensure webkit get a new FILE_URI each time.

@hciyang
Copy link

hciyang commented Jan 16, 2019

Can anyone merge this changes please?

@klodha
Copy link
Contributor Author

klodha commented Sep 7, 2019

Hi,

It has been almost a year and half, since originally pull request was created. Given it passed reviews and has required approvals, is there any chance to get it merged before iOS 13 release.

Regards

Apache Cordova: Plugin Pull Requests automation moved this from ⏳ Ready for Review to ✅ Approved, waiting for Merge Sep 16, 2019
Copy link
Member

@timbru31 timbru31 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@timbru31
Copy link
Member

Thanks for the PR @klodha. I'm going to merge it when the pipelines succeed.

@timbru31 timbru31 merged commit a9436b1 into apache:master Sep 16, 2019
Apache Cordova: Plugin Pull Requests automation moved this from ✅ Approved, waiting for Merge to 🏆 Merged, waiting for Release Sep 16, 2019
@klodha klodha deleted the klodha-camera-ios-patch branch September 16, 2019 12:45
chintan13 added a commit to logisticinfotech/cordova-plugin-camera that referenced this pull request Sep 30, 2019
* commit 'a9436b1a185b37269366ec3e2c5f337fb14cba61':
  ImagePicker returning same image (apache#306)
  chore(release): 4.1.1-dev
  chore(release): 4.1.0 (version string)
  chore(release): release notes for 4.1.0
  docs: remove outdated test docs translations
  build: remove `.ratignore` file that is not needed any more
  chore: fix repo and issue urls and license in package.json and plugin.xml
  fix: temporarily remove Appium tests (apache#468)
  ci(travis): Update Travis CI configuration for new paramedic (apache#455)
  (Android) Fix NullPointerException error on some Android phones (apache#429)
  Update CI Environment Setup for Node.js 6 (apache#438)
@jcesarmobile jcesarmobile mentioned this pull request Oct 19, 2019
3 tasks
DavidWiesner pushed a commit to DavidWiesner/cordova-plugin-camera that referenced this pull request Apr 29, 2020
ImagePicker returning same image

Co-authored-by: Jan Piotrowski <piotrowski+github@gmail.com>
Co-authored-by: Tim Brust <github@timbrust.de>
(cherry picked from commit a9436b1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Apache Cordova: Plugin Pull Requests
🏆 Merged, waiting for Release
Development

Successfully merging this pull request may close these issues.

None yet

5 participants