AssetsPickerViewController icon indicating copy to clipboard operation
AssetsPickerViewController copied to clipboard

Very slow opening of picker when media is over 8K

Open Ava8 opened this issue 4 years ago • 20 comments

I have two devices, on each is over 8K media, so when I open assets picker, I wait for а minute or more for loading image gallery.

Ava8 avatar Oct 18 '19 09:10 Ava8

I am having the same issue.

Ashu avatar Nov 02 '19 13:11 Ashu

@Ava8, @Ashu Can you please upload such images that causing the bug? I've tested with 10 images over 9MB but couldn't reproduce the problem.

DragonCherry avatar Nov 05 '19 07:11 DragonCherry

@DragonCherry I'm talking not about the quality but a count of photos and videos in the media library. So, my library is over 8000 photos and videos.

Ava8 avatar Nov 05 '19 22:11 Ava8

I looked in to this briefly, I currently use a different library which opens instantly where as this takes a few seconds before opening. I think it's due to the asset manager doing too much upfront. it loads every album and does an asset fetch request for each one.

DavidBoyes avatar Nov 07 '19 08:11 DavidBoyes

@Ava8, @Ashu , @DavidBoyes I couldn't reproduce it with iPhone 6+, over 10,000 images. "fetchAlbums" finished in very short time. Anyone can provide a fix for them?

DragonCherry avatar Nov 11 '19 07:11 DragonCherry

I use an iPhone XS Max. ~36,000 images, iCloud enabled. Definitely noticeably slower than other libraries I have used to initially open. I'm looking in to it now. fetchAlbums takes around 0.9 seconds to complete and selectAlbum around 1.4 seconds.

Overall it can take 2.5-3.5 seconds to complete viewDidLoad in AssetsPhotoViewController.

Update: func select(album newAlbum: PHAssetCollection) -> Bool Copying each asset of the fetchResult in to the array is the cause of slowness for this function I believe.

Update 2: creating the fetchMap is the next slow piece.

I removed it from the code, as well as the assetArray. This took the viewDidLoad method down to 0.12 seconds to complete. The assetArray was replaced by just using the fetchRequest directly. However syncing changes had to be commented out for this to work. I have moved the creation of fetchMap to be more on demand in fetchResult(forAlbum album: PHAssetCollection). I've had to move the creation of sortedAlbumsArray to a background thread as well. My version is currently quite different to the original due to requirements for my app so I hope these descriptions help out somehow.

DavidBoyes avatar Nov 20 '19 21:11 DavidBoyes

This issue occurred to me on a iPhone X. Currently, I don't have that device, so can't share much info about it. It works perfectly fine on my iPhone 5s with 5000 images. Also works in iPhone 6. I guess this issue occurs only on newer phones. Need to check again in new devices. Will let you know soon.

Ashu avatar Nov 21 '19 08:11 Ashu

@Ashu Is that comparing with the same image set on all devices?

DavidBoyes avatar Nov 21 '19 08:11 DavidBoyes

@Ashu Is that comparing with the same image set on all devices?

No. The image set was different. iPhone X was used by professional photographer, so it might have some RAW images. I need to check that.

Ashu avatar Nov 21 '19 13:11 Ashu

@Ashu Is that comparing with the same image set on all devices?

No. The image set was different. iPhone X was used by professional photographer, so it might have some RAW images. I need to check that.

Ok, I tried on an iPhone 6S with ~5000 images, and it was faster but I would guess relative to the amount of images/albums rather than a device specific thing.

DavidBoyes avatar Nov 21 '19 13:11 DavidBoyes

have the same issue iPhone XS having 4k++ photos. opening picker takes ~3secs. any workaround for this?

rad182 avatar Nov 25 '19 12:11 rad182

A work around that removes some of the delay (not all) is to move fetchAlbums to a background thread. You'll have to manually specify the defaultAlbum in fetchAssets() so it loads the initial set of photos.

DavidBoyes avatar Nov 27 '19 09:11 DavidBoyes

I have the same issue. 3000+ pics on my iPhone 6s and it took about 2s to open. I then wiped my iPhone and it'll open almost instantly.

zongziyao avatar Dec 09 '19 07:12 zongziyao

@DragonCherry

To get around the performance issue with loading (I have a customer with a high bar), I created a hack in my app. I load the picker's view and then stash it as a hidden view in the main view of my app. This causes it to load while the user doesn't notice. Then, I add the view controller as a child view controller and move the view back. This works surprisingly well.

My issue is that there is no way to programmatically deselect assets that are not selected in the current album. Would it be possible to make setSelectedAssets(assets: [PHAsset]) public, or to add a clearAssets() method to AssetsPhotoViewController?

avi-screenovate avatar Jul 30 '20 10:07 avi-screenovate

@avi-screenovate I'm sorry for this bug that make you use tricky mechanism. How about calling AssetsPhotoViewController.deselectAll()?

DragonCherry avatar Jul 31 '20 01:07 DragonCherry

deselectAll() only deselects from the current Album. It doesn't help for cross-album selections. In my app, I now recreate the ViewController after dismissal, to hide the load time. It's working well-enough now, and is a bit less hacky.

avi-screenovate avatar Jul 31 '20 09:07 avi-screenovate

Having this issue as well, takes a good 7 seconds for AssetsPickerViewController to load, which is quite long on my XS.

eytanschulman avatar Aug 03 '20 10:08 eytanschulman

@Ava8 @DavidBoyes @Ashu @rad182 @zongziyao @eytanschulman @DragonCherry

Hi guys, I have optimized accordingly for this issue, for more details please see :

pull request #86 pull request #85 pull request #84 develop

Please provide me with new test data and feedback to help me better refine the optimization section.

gezihuzi avatar Aug 22 '20 11:08 gezihuzi

Please consider replacing this library with PHPickerViewController.

DragonCherry avatar Sep 17 '20 04:09 DragonCherry

Please consider replacing this library with PHPickerViewController.

While native solutions are always better, PHPickerViewController is iOS 14+, many apps will need to support iOS 13 for the time being. That's why third party solutions like this one are great.

eytanschulman avatar Sep 18 '20 16:09 eytanschulman