0

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.

Abhishek Jain
  • 4,557
  • 2
  • 32
  • 31
Sharad Chauhan
  • 4,821
  • 2
  • 25
  • 50

1 Answers1

2

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
    }
}
sCha
  • 1,454
  • 1
  • 12
  • 22
  • Adding conformance to `Viewable` with an extension seems to be the way to go. Unless you need additional stored properties, a wrapping class should not be needed, and in fact avoided for performance reasons. – Raphael Sep 02 '17 at 08:36