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

Document setShouldResizeFrames #116

Open
acecilia opened this issue Oct 31, 2017 · 0 comments
Open

Document setShouldResizeFrames #116

acecilia opened this issue Oct 31, 2017 · 0 comments

Comments

@acecilia
Copy link

Please, document setShouldResizeFrames() in the Readme.

It is the only way to adapt the gif size to the screen scale (in case of the retina displays). And it is one of the main advantages of this library against the others, that in general they do not support gif resizing.

It is useful to highlight the scaling possibility to the retina displays, so the devs are aware that it is not automatically supported, but can be implemented easily.

The needed steps are:

  • Read the @1x gif into a UIImage and get the frame.size.
  • Set the size of the GIFImageView to the previous obtained size, scaling it using UIScreen.main.scale.
  • Add the line setShouldResizeFrames(true).
  • Pass the @2x or @3x file names to prepareForAnimation depending on UIScreen.main.scale.

Example:

extension GIFImageView {
    func customPrepareForAnimation(withGIFNamed imageName: String, loopCount: Int = 1) {
        let scale = UIScreen.main.scale
        var scaledImageName = imageName

        if Int(scale) == 2 {
            scaledImageName += "@2x"
        } else if Int(scale) >= 3 {
            scaledImageName += "@3x"
        }

        if let url = Bundle.main.url(forResource: scaledImageName, withExtension: "gif"), let data = try? Data(contentsOf: url), let image = UIImage(data: data) {
            frame = CGRect(x: 0, y: 0, width: image.size.width / scale, height: image.size.height / scale)
            setShouldResizeFrames(true)
        } else {
            assertionFailure("Imposible to load \(scaledImageName).gif from main bundle. Check the resource name and try again")
        }

        prepareForAnimation(withGIFNamed: scaledImageName, loopCount: loopCount)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants