Use Javascript to fetch to the image for the ImageViewController
This commit is contained in:
parent
a1f26898c8
commit
4759cedd35
@ -349,13 +349,36 @@ extension ArticleViewController: WKUIDelegate {
|
|||||||
extension ArticleViewController: WKScriptMessageHandler {
|
extension ArticleViewController: WKScriptMessageHandler {
|
||||||
|
|
||||||
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
||||||
if message.name == MessageName.imageWasClicked, let link = message.body as? String, let url = URL(string: link) {
|
if message.name == MessageName.imageWasClicked,
|
||||||
let imageVC = UIStoryboard.main.instantiateController(ofType: ImageViewController.self)
|
let body = message.body as? String,
|
||||||
imageVC.url = url
|
let data = body.data(using: .utf8),
|
||||||
imageVC.modalPresentationStyle = .fullScreen
|
let clickMessage = try? JSONDecoder().decode(ImageClickMessage.self, from: data),
|
||||||
present(imageVC, animated: true)
|
let range = clickMessage.imageURL.range(of: ";base64,") {
|
||||||
|
|
||||||
|
let base64Image = String(clickMessage.imageURL.suffix(from: range.upperBound))
|
||||||
|
if let imageData = Data(base64Encoded: base64Image), let image = UIImage(data: imageData) {
|
||||||
|
let imageVC = UIStoryboard.main.instantiateController(ofType: ImageViewController.self)
|
||||||
|
imageVC.image = image
|
||||||
|
imageVC.modalPresentationStyle = .fullScreen
|
||||||
|
present(imageVC, animated: true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: JSON
|
||||||
|
private struct TemplateData: Codable {
|
||||||
|
let style: String
|
||||||
|
let body: String
|
||||||
|
}
|
||||||
|
|
||||||
|
private struct ImageClickMessage: Codable {
|
||||||
|
let x: Float
|
||||||
|
let y: Float
|
||||||
|
let width: Float
|
||||||
|
let height: Float
|
||||||
|
let imageURL: String
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: Private
|
// MARK: Private
|
||||||
@ -369,8 +392,3 @@ private extension ArticleViewController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private struct TemplateData: Codable {
|
|
||||||
let style: String
|
|
||||||
let body: String
|
|
||||||
}
|
|
||||||
|
@ -69,6 +69,7 @@ class ArticleViewControllerWebViewProvider: NSObject, WKNavigationDelegate {
|
|||||||
|
|
||||||
let configuration = WKWebViewConfiguration()
|
let configuration = WKWebViewConfiguration()
|
||||||
configuration.preferences = preferences
|
configuration.preferences = preferences
|
||||||
|
configuration.setValue(true, forKey: "allowUniversalAccessFromFileURLs")
|
||||||
configuration.allowsInlineMediaPlayback = true
|
configuration.allowsInlineMediaPlayback = true
|
||||||
configuration.mediaTypesRequiringUserActionForPlayback = .video
|
configuration.mediaTypesRequiringUserActionForPlayback = .video
|
||||||
|
|
||||||
|
@ -11,55 +11,29 @@ import UIKit
|
|||||||
class ImageViewController: UIViewController {
|
class ImageViewController: UIViewController {
|
||||||
|
|
||||||
@IBOutlet weak var shareButton: UIButton!
|
@IBOutlet weak var shareButton: UIButton!
|
||||||
@IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!
|
|
||||||
@IBOutlet weak var imageScrollView: ImageScrollView!
|
@IBOutlet weak var imageScrollView: ImageScrollView!
|
||||||
|
|
||||||
private var dataTask: URLSessionDataTask? = nil
|
var image: UIImage!
|
||||||
private var image: UIImage?
|
|
||||||
var url: URL!
|
|
||||||
|
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
|
|
||||||
activityIndicatorView.isHidden = false
|
|
||||||
activityIndicatorView.startAnimating()
|
|
||||||
|
|
||||||
imageScrollView.setup()
|
imageScrollView.setup()
|
||||||
imageScrollView.imageScrollViewDelegate = self
|
imageScrollView.imageScrollViewDelegate = self
|
||||||
imageScrollView.imageContentMode = .aspectFit
|
imageScrollView.imageContentMode = .aspectFit
|
||||||
imageScrollView.initialOffset = .center
|
imageScrollView.initialOffset = .center
|
||||||
|
imageScrollView.display(image: image)
|
||||||
dataTask = URLSession.shared.dataTask(with: url) { [weak self] data, response, error in
|
|
||||||
guard let self = self else { return }
|
|
||||||
|
|
||||||
if let data = data, let image = UIImage(data: data) {
|
|
||||||
|
|
||||||
self.image = image
|
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
self.shareButton.isEnabled = true
|
|
||||||
self.activityIndicatorView.isHidden = true
|
|
||||||
self.activityIndicatorView.stopAnimating()
|
|
||||||
self.imageScrollView.display(image: image)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
dataTask!.resume()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@IBAction func share(_ sender: Any) {
|
@IBAction func share(_ sender: Any) {
|
||||||
guard let image = image else { return }
|
guard let image = image else { return }
|
||||||
let activityViewController = UIActivityViewController(activityItems: [url!, image], applicationActivities: nil)
|
let activityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
|
||||||
activityViewController.popoverPresentationController?.sourceView = shareButton
|
activityViewController.popoverPresentationController?.sourceView = shareButton
|
||||||
activityViewController.popoverPresentationController?.sourceRect = shareButton.bounds
|
activityViewController.popoverPresentationController?.sourceRect = shareButton.bounds
|
||||||
present(activityViewController, animated: true)
|
present(activityViewController, animated: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
@IBAction func done(_ sender: Any) {
|
@IBAction func done(_ sender: Any) {
|
||||||
dataTask?.cancel()
|
|
||||||
dismiss(animated: true)
|
dismiss(animated: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -237,19 +237,10 @@
|
|||||||
<subviews>
|
<subviews>
|
||||||
<scrollView verifyAmbiguity="off" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" ambiguous="YES" translatesAutoresizingMaskIntoConstraints="NO" id="msG-pz-EKk" customClass="ImageScrollView" customModule="NetNewsWire" customModuleProvider="target">
|
<scrollView verifyAmbiguity="off" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" ambiguous="YES" translatesAutoresizingMaskIntoConstraints="NO" id="msG-pz-EKk" customClass="ImageScrollView" customModule="NetNewsWire" customModuleProvider="target">
|
||||||
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
|
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
|
||||||
<subviews>
|
|
||||||
<activityIndicatorView opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" style="large" translatesAutoresizingMaskIntoConstraints="NO" id="iEh-n3-Vkg">
|
|
||||||
<rect key="frame" x="188.5" y="390.5" width="37" height="37"/>
|
|
||||||
</activityIndicatorView>
|
|
||||||
</subviews>
|
|
||||||
<constraints>
|
|
||||||
<constraint firstItem="iEh-n3-Vkg" firstAttribute="centerX" secondItem="msG-pz-EKk" secondAttribute="centerX" id="FSP-DY-Vax"/>
|
|
||||||
<constraint firstItem="iEh-n3-Vkg" firstAttribute="centerY" secondItem="msG-pz-EKk" secondAttribute="centerY" id="rev-zC-wMY"/>
|
|
||||||
</constraints>
|
|
||||||
<viewLayoutGuide key="contentLayoutGuide" id="phv-DN-krZ"/>
|
<viewLayoutGuide key="contentLayoutGuide" id="phv-DN-krZ"/>
|
||||||
<viewLayoutGuide key="frameLayoutGuide" id="NNU-C8-Fsz"/>
|
<viewLayoutGuide key="frameLayoutGuide" id="NNU-C8-Fsz"/>
|
||||||
</scrollView>
|
</scrollView>
|
||||||
<button opaque="NO" contentMode="scaleToFill" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="RmY-a3-hUg">
|
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="RmY-a3-hUg">
|
||||||
<rect key="frame" x="362" y="44" width="44" height="44"/>
|
<rect key="frame" x="362" y="44" width="44" height="44"/>
|
||||||
<constraints>
|
<constraints>
|
||||||
<constraint firstAttribute="width" constant="44" id="56o-Zr-T0w"/>
|
<constraint firstAttribute="width" constant="44" id="56o-Zr-T0w"/>
|
||||||
@ -288,7 +279,6 @@
|
|||||||
<viewLayoutGuide key="safeArea" id="mbY-02-GFL"/>
|
<viewLayoutGuide key="safeArea" id="mbY-02-GFL"/>
|
||||||
</view>
|
</view>
|
||||||
<connections>
|
<connections>
|
||||||
<outlet property="activityIndicatorView" destination="iEh-n3-Vkg" id="xue-X1-awS"/>
|
|
||||||
<outlet property="imageScrollView" destination="msG-pz-EKk" id="dGi-M6-dcO"/>
|
<outlet property="imageScrollView" destination="msG-pz-EKk" id="dGi-M6-dcO"/>
|
||||||
<outlet property="shareButton" destination="RmY-a3-hUg" id="Z54-ah-WAI"/>
|
<outlet property="shareButton" destination="RmY-a3-hUg" id="Z54-ah-WAI"/>
|
||||||
</connections>
|
</connections>
|
||||||
|
@ -1,6 +1,34 @@
|
|||||||
// Used to pop a resizable image view
|
// Used to pop a resizable image view
|
||||||
function imageWasClicked(img) {
|
async function imageWasClicked(img) {
|
||||||
window.webkit.messageHandlers.imageWasClicked.postMessage(img.src);
|
const rect = img.getBoundingClientRect();
|
||||||
|
|
||||||
|
var message = {
|
||||||
|
x: rect.x,
|
||||||
|
y: rect.y,
|
||||||
|
width: rect.width,
|
||||||
|
height: rect.height
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(img.src);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Network response was not ok.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const imgBlob = await response.blob();
|
||||||
|
|
||||||
|
var reader = new FileReader();
|
||||||
|
reader.readAsDataURL(imgBlob);
|
||||||
|
|
||||||
|
reader.onloadend = function() {
|
||||||
|
message.imageURL = reader.result;
|
||||||
|
var jsonMessage = JSON.stringify(message);
|
||||||
|
window.webkit.messageHandlers.imageWasClicked.postMessage(jsonMessage);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log('There has been a problem with your fetch operation: ', error.message);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the click listeners for images
|
// Add the click listeners for images
|
||||||
|
Loading…
x
Reference in New Issue
Block a user