Mahan
Mahan

Reputation: 157

Zoomable UIImageView

I'm using this library for zoomable UIImageView.

Problem: When image width is smaller than screen width, it aligns to the left of the screen. After first attempt to zoom it is corrected.

I've searched and tried lots of answers and tried to fix this issue but seems its using the same way others propose.

here's the link to library source code : https://github.com/huynguyencong/ImageScrollView/blob/master/Sources/ImageScrollView.swift

Upvotes: 0

Views: 131

Answers (1)

sundance
sundance

Reputation: 3020

Why don't you use a simple UIWebView? This way you do not require any framework:

@IBOutlet weak var webView: UIWebView!

func loadImage(url: URL) {
    self.webView.stopLoading()
            let html = "<!DOCTYPE html>" +
            "<html>" +
            "<head>" +
            "<meta charset=\"UTF-8\">" +
            "<style type=\"text/css\">" +
            "img{position: absolute; top: 0; bottom: 0; left: 0; right: 0; margin: auto; max-width: 100%; max-height: 100%;}" +
            "</style>" +
            "</head>" +
            "<body>" +
            "<img src='\(url)'/>" +
            "</body>" +
            "</html>"
    self.webView.loadHTMLString(html, baseURL: nil)
}

You can load files with remote URLs as well as files with local URLs. You can get a local URL for example like this:

let url = URL(fileURLWithPath: Bundle.main.path(forResource: "image", ofType: "png")!)!

Upvotes: 1

Related Questions