简体   繁体   中英

Collection of items that conform to the protocol ? : Viewer Swift

I am trying to integrate Viewer library to see photos and videos from album. I have integrated everything but one. I don't understand what this line means on there github link : You'll need a collection of items that comform to the Viewable protocol.

Viewable Protocol :

public enum ViewableType: String {
    case image
    case video
}

public protocol Viewable {
    var type: ViewableType { get }
    var assetID: String? { get }
    var url: String? { get }
    var placeholder: UIImage { get }

    func media(_ completion: @escaping (_ image: UIImage?, _ error: NSError?) -> Void)
}

And this is how we have to use this library :

extension CollectionController: ViewerControllerDataSource {
    func viewerController(_ viewerController: ViewerController, viewableAt indexPath: IndexPath) -> Viewable {
        return photos[indexPath.row]
    }
}

In this extension we have to return Viewable and thats my problem. I have PHAssets but need to make Viewable type. How to do it ?

For reference check Viewer library : https://github.com/bakkenbaeck/Viewer

Any help would be appreciated.

I believe you'll need something like below:

extension PHAsset: Viewable {
    var type: ViewableType { return .image }
    var assetID: String? { return "item's id" } 
    var url: String? { return "item's url" }
    var placeholder: UIImage { return defaultImage }

    func media(...) {
        // implement this function
    }
}

or you may create your own class:

class MyAsset: Viewable {
    var asset: PHAsset
    var type: ViewableType { return .image }

    ... and others ...

    init(asset: PHAsset) {
        self.asset = asset
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM