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

能否打开指定相册 #647

Closed
liumengdi666 opened this issue Apr 3, 2024 · 6 comments · Fixed by #650
Closed

能否打开指定相册 #647

liumengdi666 opened this issue Apr 3, 2024 · 6 comments · Fixed by #650

Comments

@liumengdi666
Copy link

有一个需求,传入指定的相册名称,打开默认选中的是对应的相册浏览,看了下api貌似并没有对应参数,能否支持下?

@SilenceLove
Copy link
Owner

SilenceLove commented Apr 3, 2024

/// 获取相机胶卷相册的 `PhotoAssetCollection` 对象
/// 打开选择器默认显示的相册
static func fetchCameraAssetCollection(
_ config: PickerConfiguration,
options: PHFetchOptions
) -> PhotoAssetCollection?

config.fetchAssetCollection = TestPhotoFetchAssetCollection.self

public struct TestPhotoFetchAssetCollection: PhotoFetchAssetCollection {
    public static func fetchCameraAssetCollection(_ config: PickerConfiguration, options: PHFetchOptions) -> PhotoAssetCollection? {
        if /*自定义默认显示的相册*/ {
            if config.creationDate {
                options.sortDescriptors = [
                    NSSortDescriptor(
                        key: "creationDate",
                        ascending: config.creationDate
                    )
                ]
            }
            var collection: PhotoAssetCollection?
            fetchAssetCollections(options: options) { assetCollection, isCameraRoll, stop in
                if !isCameraRoll {
                    collection = assetCollection
                    stop.pointee = true
                }
            }
            if let collection {
                return collection
            }
        }
        return DefaultPhotoFetchAssetCollection.fetchCameraAssetCollection(config, options: options)
    }
}

@liumengdi666
Copy link
Author

liumengdi666 commented Apr 7, 2024

你好, 按照上边的方式实现了, 我们采用albumShowMode方式为popup,有点小问题, 比如自定义相册为'xxx', 弹出后点击上边的标题展示albumList会发现显示的是'xxx',但是列表选中的却是'最近项目', 切换几次后'最近项目'又不见被替换为'xxx'了,这时列表里有2个'xxx'相册, 虽然可以修改自定义相册collection.isSelected = true稳定这种情况, 但是我们希望弹出相册列表时默认选中的是'xxx',同时'最近项目'也存在,这样可以实现吗?

        _ config: PickerConfiguration,
        options: PHFetchOptions
    ) -> PhotoAssetCollection? {
        if config.creationDate {
            options.sortDescriptors = [
                NSSortDescriptor(
                    key: "creationDate",
                    ascending: config.creationDate
                )
            ]
        }
        var collection: PhotoAssetCollection?
        fetchAssetCollections(options: options) { assetCollection, isCameraRoll, stop in
            if !isCameraRoll, assetCollection.albumName == "xxx" {
                collection = assetCollection
                stop.pointee = true
            }
        }
        if let collection {
//            collection.isSelected = true
            return collection
        }
        return DefaultPhotoFetchAssetCollection.fetchCameraAssetCollection(config, options: options)
    }```
![image](https://github.com/SilenceLove/HXPhotoPicker/assets/12481783/5d31afad-848d-4c9b-8fcd-b3db4aa899e4)



@liumengdi666
Copy link
Author

liumengdi666 commented Apr 7, 2024

我发现可以自定义PhotoFetchData,但是在重写fetchAssetCollections时遇到了一些问题,
image
这两个属性没有public,重写的时候无法使用.
我发现'最近项目'是下边这句代码替换掉的
image
所以我想重写这个方法,不知道重写这个方法是否可行,感觉重写后的select状态依然有些混乱.

@SilenceLove
Copy link
Owner

你好, 按照上边的方式实现了, 我们采用albumShowMode方式为popup,有点小问题, 比如自定义相册为'xxx', 弹出后点击上边的标题展示albumList会发现显示的是'xxx',但是列表选中的却是'最近项目', 切换几次后'最近项目'又不见被替换为'xxx'了,这时列表里有2个'xxx'相册, 虽然可以修改自定义相册collection.isSelected = true稳定这种情况, 但是我们希望弹出相册列表时默认选中的是'xxx',同时'最近项目'也存在,这样可以实现吗?

        _ config: PickerConfiguration,
        options: PHFetchOptions
    ) -> PhotoAssetCollection? {
        if config.creationDate {
            options.sortDescriptors = [
                NSSortDescriptor(
                    key: "creationDate",
                    ascending: config.creationDate
                )
            ]
        }
        var collection: PhotoAssetCollection?
        fetchAssetCollections(options: options) { assetCollection, isCameraRoll, stop in
            if !isCameraRoll, assetCollection.albumName == "xxx" {
                collection = assetCollection
                stop.pointee = true
            }
        }
        if let collection {
//            collection.isSelected = true
            return collection
        }
        return DefaultPhotoFetchAssetCollection.fetchCameraAssetCollection(config, options: options)
    }```
![image](https://github.com/SilenceLove/HXPhotoPicker/assets/12481783/5d31afad-848d-4c9b-8fcd-b3db4aa899e4)

这个问题下个版本会修改,不会替换掉第一个相册

@SilenceLove SilenceLove linked a pull request Apr 8, 2024 that will close this issue
@liumengdi666
Copy link
Author

试了最新的修改代码后相册已经不会被替换掉了, 然而发现有一个bug:

https://github.com/SilenceLove/HXPhotoPicker/assets/12481783/927e7131-99b0-4a49-bbb0-dc5862c44434
视频中可以看到打开自定义相册后选中状态有些错乱, 我调试了下大概知道原因:
image

self.cameraAssetCollection可能还没拿到的时候已经走到这个if判断里, 看起来2个queue执行完毕的先后顺序并不能确定,所以时好时坏.

SilenceLove added a commit that referenced this issue Apr 9, 2024
@SilenceLove
Copy link
Owner

最新的代码试试

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants