ivpusic / react-native-image-crop-picker

iOS/Android image picker with support for camera, video, configurable compression, multiple images and cropping
MIT License
6.08k stars 1.55k forks source link

iOS: images in picker grid get invisible if the access mode is "limited" (selected photos only) #1641

Open vtt2021 opened 3 years ago

vtt2021 commented 3 years ago

Version

Tell us which versions you are using:

Platform

Tell us to which platform this issue is related

Expected behaviour

all thumbnails remain visible all the time

Actual behaviour

thumbnail images in picker grid get invisible one after another

Steps to reproduce

  1. "Selected photos only" mode is active.
  2. open picker with ImagePicker.openPicker({ includeExif: true, compressImageQuality: 1, mediaType: 'photo', forceJpg: true })
  3. if the app is opened for the first time, the first screen will be the grid that allows to select images that are accessible. select some images, e.g. 10-20
  4. after closing this OS picker, there will remain the react-native-image-crop-picker grid visible. open "recents"
  5. then the issue happens. at the very first moment, all thumbnails are shown, but in half of a second, arbitrary thumbnails start disappearing one after another.
  6. it is still possible to select the hidden images (black square space, no image) and all the next processing works fine.

Note: if during first load user selects "All" as access mode, then everything works fine, until it is not switched to "Limited" in settings. then the issue is there and switching back to "All" doesn't help.

Note 2: iCloud storage is enabled with option "Optimize iPhone Storage" in Settings / Photos

Attachments

// stacktrace or any other useful debug info

Love react-native-image-crop-picker? Please consider supporting our collective: 👉 https://opencollective.com/react-native-image-crop-picker/donate

munibkhalidhere commented 3 years ago

have you found the solution?

WillTinney commented 2 years ago

Still having this issue

dnnp2011 commented 2 years ago

I think I'm also having this problem. Thumbnails turn into white squares after a split second.

vtt2021 commented 2 years ago

After some investigations I found out that images are getting disappeared because these are backed in iCloud. To make it working, a special flag should be used for requestImageForAsset method: https://stackoverflow.com/questions/31037859/phimagemanager-requestimageforasset-returns-nil-sometimes-for-icloud-photos

So in QBAssetsViewController.m [line 474] I've defined options for the request that have networkAccessAllowed set to YES. This fixed the issue, but presented another. Images in the list started "dancing" - appearing and disappearing in different cells, missplacing and sometimes duplicating. But after some time, the list becomes completely correct. If I use "synchronous" as YES in options, then the behavior is completely fine, but another issue is presented: UI freezes for seconds while images are loading. So it is not a solution, but it clearly shows that the issue is that after an image is retrieved, the UI rebuilds and order is broken. So I started playing with sorting options but still no luck.

So I decided to move another direction from start. Since all images are shown for a moment and then get lost, I've decided to try to prevent it from getting disappeared. I've found that updateCachedAssets method is doing the thing. And well, good news, simple commenting it out makes it working!

Here is the fix (QBAssetsViewController.m):

  1. Since the issue only reproduces in limited mode, it is reasonable to scope the fix to limited mode only. So add this piece of code into the start (line 21):

    static bool isLimitedPermission() {
    if (@available(iOS 14, *)) {
        PHAuthorizationStatus accessLevel = [PHPhotoLibrary authorizationStatusForAccessLevel: PHAccessLevelReadWrite];
        return accessLevel == PHAuthorizationStatusLimited;
    }
    return false;
    }
  2. Disable updateCachedAssets in limited mode (line 315): if (isLimitedPermission()) { return; }

PS. One minor issue I found having this fix enabled is that when you are on items list page and then drag and drop the page ( using left side of the page) leads to items flicker for a moment. This could be fixed by commenting out all invokations of [self.collectionView reloadData];

PPS. I'm not an ios native developer and this is my first try, so I can't promise this is a good fix. Anybody with relevant experience is welcome to this thread!

Here is final file (remove .txt extension yourself): QBAssetsViewController.m.txt