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

update sharing/storing best practice #243

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 27 additions & 30 deletions Docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
- [GiphyDelegate](#events)

**GPHMedia**
- [Accessing Media Assets](#accessing-media-assets)
- [GPHMediaView](#gphmediaview)
- [Media IDs](#media-ids)
- [Sharing and Storing](#sharing-and-storing)

**Caching & Dependencies**
- [Caching](#caching)
Expand Down Expand Up @@ -253,48 +254,44 @@ extension YourController: GiphyDelegate {

From there, it's up to you to decide what to do with the GIF!

### _Accessing Media Assets_

### _GPHMediaView_
Every `GPHMedia` object provides a collection of renditions, which are distinct versions of the media asset in a variety of resolutions and file types. We recommend sticking with the `webp` file format, which loads fast and looks great.

Create a `GPHMediaView` to display the media:
Once a gif is selected from the grid, use the `fixed_width` webp rendition for display in social contexts like message threads or comments.

```swift
let mediaView = GPHMediaView()
mediaView.media = media
```
let url: String? = media.url(rendition: .fixedWidth, fileType: .webp)
```
For larger display areas and creation contexts, consider the `original` webp rendition.

Use the media's `aspectRatio` property to size the view:
```swift
let aspectRatio = media.aspectRatio
```
let url: String? = media.url(rendition: .original, fileType: .webp)
```

If you want to build your own view to display a GIF, grab a URL to the asset like so:
```swift
let webpURL = media.url(rendition: .original, fileType: .webp)
let gifURL = media.url(rendition: .fixedWidth, fileType: .gif)
let vidURL = media.url(rendition: .fixedWidth, fileType: .mp4)
### _GPHMediaView_

let url = URL(string: gifURL)
```
Create a `GPHMediaView` to display the asset:

### _Media IDs_
```swift
let mediaView = GPHMediaView()
mediaView.loadAsset(at: url)
```

In a messaging app context, you may want to send media `id`s rather than `GPHMedia` objects or image assets.
### _Sharing and Storing_

Obtain a `GPHMedia`'s `id` property via `media.id`
Store the unmodified media url on your app's backend in order to make a gif selected by one user available to others.

This is in contrast to the approach of storing/sharing the gif's id and calling the `get gif by id` endpoint on the receiver(s) end to fetch the gif object, which we no longer suggest:

On the receiving end, obtain a `GPHMedia` from the `id` like so:

```swift
GiphyCore.shared.gifByID(id) { (response, error) in
if let media = response?.data {
DispatchQueue.main.sync { [weak self] in
self?.mediaView.media = media
}
}
}
```
~~### _Media IDs_~~
~~In a messaging app context, you may want to send media `id`s rather than `GPHMedia` objects or image assets.~~
~~Obtain a `GPHMedia`'s `id` property via `media.id`~~
~~On the receiving end, obtain a `GPHMedia` from the `id` like so:~~
~~`GiphyCore.shared.gifByID(`id) { (response, error) in }`~~

Rendition URLs are stable and designed to provide long-term access to the associated GIFs, as long as they remain publicly available on GIPHY's library.

### _Caching_
We use [URLCache](https://developer.apple.com/documentation/foundation/urlcache) to cache media assets, which reduces unnecessary image requests and loading times.

Expand Down