capacitor-community / photoviewer

⚡ Capacitor plugin to view table images with fullscreen and sharing capabilities.
MIT License
54 stars 27 forks source link

Exception thrown while sharing on iOS using capacitor:// #52

Closed chiraganand closed 11 months ago

chiraganand commented 1 year ago

I am using capacitor:// protocol. The plugin is able to use this to show an image (single image) but throws when trying to share it.

Log:

2023-09-16 22:11:00.433030+0530 App[6179:690925] Task <9BF2E522-BCA5-48BF-B85B-1E7D14F0D65D>.<2> finished with error [-1002] Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSErrorFailingURLStringKey=capacitor://localhost/_capacitor_file_/var/mobile/Containers/Data/Application/4FF99DC9-9CCA-4BF3-B3EB-F2E27E3D5A67/Library/NoCloud/download/Img_file.jpg, NSErrorFailingURLKey=capacitor://localhost/_capacitor_file_/var/mobile/Containers/Data/Application/4FF99DC9-9CCA-4BF3-B3EB-F2E27E3D5A67/Library/NoCloud/download/Img_file.jpg, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <9BF2E522-BCA5-48BF-B85B-1E7D14F0D65D>.<2>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <9BF2E522-BCA5-48BF-B85B-1E7D14F0D65D>.<2>, NSUnderlyingError=0x2829dd140 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}
jepiqueau commented 1 year ago

@chiraganand have you done this

- in Xcode, open `Info.plist` and add a new Information Property like `Privacy - Photo Library Usage Description` and set a value to `We need to write photos`. This is required to have the `Share`of images and the `create Movie` working.
chiraganand commented 1 year ago

@chiraganand have you done this

- in Xcode, open `Info.plist` and add a new Information Property like `Privacy - Photo Library Usage Description` and set a value to `We need to write photos`. This is required to have the `Share`of images and the `create Movie` working.

Yes, this already exists in Info.plist:

        <key>NSPhotoLibraryAddUsageDescription</key>
        <string>For saving images</string>
        <key>NSPhotoLibraryUsageDescription</key>
        <string>For image sharing</string>
chiraganand commented 1 year ago

Alright, so I debugged it and got it working by modifying OneImageViewController::shareButtonTapped():

        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.clipsToBounds = true
-      imageView.sd_setImage(with: URL(string: url), placeholderImage: nil)
+      let sUrl = url.replacingOccurrences(of: "capacitor://localhost/_capacitor_file_", with: "")
+      imageView.sd_setImage(with: URL(fileURLWithPath: sUrl), placeholderImage: nil)

May not be the best way to fix this because maybe a class member can be created to hold this value, perhaps.

I can create a PR with this if this looks okay.

jepiqueau commented 1 year ago

@chiraganand can you give me the full url so i can have a look

chiraganand commented 1 year ago

@chiraganand can you give me the full url so i can have a look

Do you mean full URL of the file? capacitor://localhost/_capacitor_file_/var/mobile/Containers/Data/Application/4FF99DC9-9CCA-4BF3-B3EB-F2E27E3D5A67/Library/NoCloud/download/Img_file.jpg

jepiqueau commented 1 year ago

@chiraganand i test this

      this.imageList.push({url: 'capacitor://localhost/_capacitor_file_/var/mobile/Media/DCIM/100APPLE/IMG_0013.JPG', title: 'Image13'});

when i am in gallery i got the image i click on it and i click on the button share and as i am on an emulator i select copy image and when i go to the photo application i can see that the image has been copied and if i ask for info i got IMG_0015

so for me it is working so this is why i was asking you the url of your image

chiraganand commented 1 year ago

In my case, the image is already stored in the application directory and when I view that is when this issue happens. Perhaps, something to do with the path.

Also, when I try to run my app in a simulator I am not able to even view the photo because the getImage() function requires "mobile" in the path which is not present in the simulator so it shows the placeholder image. I fixed this by replacing "mobile" with "data".

So, I am also guessing this could be something to do with the path.

jepiqueau commented 1 year ago

@chiraganand How did you create the image Img_file.jpg in Library/NoCloud/download

chiraganand commented 1 year ago

I am downloading the image from a remote server and then saving it using the Cordova File plugin.

jepiqueau commented 1 year ago

@chiraganand i am doing the same except that i do not use the cordova file plugin but some functions that i implement myself in photoviewer so i save the image in 'capacitor://localhost/_capacitorfile/var/mobile/Containers/Data/Application/88CDCA9F-627C-4554-A5BC-BC7A55FA7D93/Library/Images/Download/test_image' and when i pass that url in the imageList i saw the image in the gallery and when i click on it and click on the share button it works like expected

jepiqueau commented 1 year ago

@chiraganand in the release 3.0.4 i have added the feature to load an image from http request and save it internally to the device having load an image go to the slider share the image and it works fine so i cannot reproduce your case

jepiqueau commented 1 year ago

@chiraganand hi, did you try 3.0.4

jepiqueau commented 11 months ago

@chiraganand As you did not come back i assume that it is working. So i close the issue. Feel free to re-open if needed