Skip to content

Issue with random image order in selected assets #115

Description

@HenryDouglasZLabs

Hi Team,

I am facing an issue with the AssetsPickerViewController where the selected assets in the assetsPicker(controller:selected:) method are coming in a random order instead of the order in which they were selected.

Environment:

  • AssetsPickerViewController: 2.9.7
  • Platform: iOS 16
  • Xcode: 14.2

Description:
In my implementation of the assetsPicker(controller:selected:) method, the selected assets are not in the expected order. I expect them to be in the order in which I selected them, but they appear in a random order.

func openPhotoLibrary(imageLimit: Int) {
let pickOptions = PHFetchOptions()
pickOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)
pickOptions.sortDescriptors = [
NSSortDescriptor(key: "creationDate", ascending: false),
]

    let pickerConfig = AssetsPickerConfig()
    pickerConfig.assetsIsScrollToBottom = false
    pickerConfig.assetCellType = CustomAssetCell.classForCoder()
    pickerConfig.assetPortraitColumnCount = 3
    pickerConfig.assetLandscapeColumnCount = 5
    pickerConfig.assetsMaximumSelectionCount = imageLimit
    
    let picker = AssetsPickerViewController()
    picker.pickerConfig = pickerConfig
    picker.pickerDelegate = self
    picker.pickerConfig.assetFetchOptions = [
    .smartAlbum: pickOptions,
    .album: pickOptions
    ]

    picker.modalPresentationStyle = .fullScreen
    self.imageLimit = imageLimit
    present(picker, animated: true, completion: nil)

}

func assetsPicker(controller: AssetsPickerViewController, selected assets: [PHAsset]) {func assetsPicker(controller: AssetsPickerViewController, selected assets: [PHAsset]) {
var images: [UIImage] = []
let options = PHImageRequestOptions()
options.isSynchronous = false // Set to true if you want to make the request synchronous
options.deliveryMode = .highQualityFormat

    let group = DispatchGroup() // Use a dispatch group to track the completion of all requests
    
    for item in assets {
        group.enter() // Enter the group before starting each request
        
        PHImageManager.default().requestImage(for: item,
                                              targetSize: PHImageManagerMaximumSize,
                                              contentMode: .aspectFit,
                                              options: options) { (image, info) in
            images.append(image ?? UIImage())
            group.leave() // Leave the group after each request is completed
        }
    }
    
    group.notify(queue: DispatchQueue.main) {
        // All requests are completed
        guard !images.isEmpty else {
            return
        }
        (self.viewModel as! PhotosViewModel).storePhotosInRealm(images: images)
    }
}    // Issue is observed with the order of assets in the 'assets' array

}

@DragonCherry @felixbuenemann @stefanrenne @ebrenesc @foxware00

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions