Add share button to full screen image view.

This commit is contained in:
Maurice Parker 2019-10-12 15:27:38 -05:00
parent 3ee0506b4a
commit 60fb58ecaa
3 changed files with 34 additions and 2 deletions

View File

@ -10,10 +10,12 @@ import UIKit
class ImageViewController: UIViewController {
@IBOutlet weak var shareButton: UIButton!
@IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!
@IBOutlet weak var imageScrollView: ImageScrollView!
private var dataTask: URLSessionDataTask? = nil
private var image: UIImage?
var url: URL!
override func viewDidLoad() {
@ -29,9 +31,13 @@ class ImageViewController: UIViewController {
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)
@ -44,7 +50,15 @@ class ImageViewController: UIViewController {
dataTask!.resume()
}
@IBAction func share(_ sender: Any) {
guard let image = image else { return }
let activityViewController = UIActivityViewController(activityItems: [url!, image], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = shareButton
present(activityViewController, animated: true)
}
@IBAction func done(_ sender: Any) {
dataTask?.cancel()
dismiss(animated: true)
}

View File

@ -249,8 +249,20 @@
<viewLayoutGuide key="contentLayoutGuide" id="phv-DN-krZ"/>
<viewLayoutGuide key="frameLayoutGuide" id="NNU-C8-Fsz"/>
</scrollView>
<button opaque="NO" contentMode="scaleToFill" enabled="NO" 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"/>
<constraints>
<constraint firstAttribute="width" constant="44" id="56o-Zr-T0w"/>
<constraint firstAttribute="height" constant="44" id="lBg-Wy-k9P"/>
</constraints>
<color key="tintColor" name="primaryAccentColor"/>
<state key="normal" image="square.and.arrow.up.fill" catalog="system"/>
<connections>
<action selector="share:" destination="vO9-a3-Dnu" eventType="touchUpInside" id="m8y-Gs-YF1"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="cXR-ll-xBx">
<rect key="frame" x="0.0" y="44" width="44" height="44"/>
<rect key="frame" x="8" y="44" width="44" height="44"/>
<constraints>
<constraint firstAttribute="height" constant="44" id="6kc-Gw-KbZ"/>
<constraint firstAttribute="width" constant="44" id="cBq-gs-WzN"/>
@ -264,11 +276,13 @@
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<constraints>
<constraint firstItem="RmY-a3-hUg" firstAttribute="top" secondItem="mbY-02-GFL" secondAttribute="top" id="A0i-Hs-1Ac"/>
<constraint firstItem="mbY-02-GFL" firstAttribute="bottom" secondItem="msG-pz-EKk" secondAttribute="bottom" id="AtA-bA-jDr"/>
<constraint firstItem="mbY-02-GFL" firstAttribute="trailing" secondItem="msG-pz-EKk" secondAttribute="trailing" id="R49-qV-8nm"/>
<constraint firstItem="msG-pz-EKk" firstAttribute="leading" secondItem="mbY-02-GFL" secondAttribute="leading" id="XN1-xN-hYS"/>
<constraint firstItem="mbY-02-GFL" firstAttribute="trailing" secondItem="RmY-a3-hUg" secondAttribute="trailing" constant="8" id="Zlz-lM-LV8"/>
<constraint firstItem="msG-pz-EKk" firstAttribute="top" secondItem="mbY-02-GFL" secondAttribute="top" id="p1a-s0-wdK"/>
<constraint firstItem="cXR-ll-xBx" firstAttribute="leading" secondItem="mbY-02-GFL" secondAttribute="leading" id="vJs-LN-Ydd"/>
<constraint firstItem="cXR-ll-xBx" firstAttribute="leading" secondItem="mbY-02-GFL" secondAttribute="leading" constant="8" id="vJs-LN-Ydd"/>
<constraint firstItem="cXR-ll-xBx" firstAttribute="top" secondItem="mbY-02-GFL" secondAttribute="top" id="xVN-Qt-WYA"/>
</constraints>
<viewLayoutGuide key="safeArea" id="mbY-02-GFL"/>
@ -276,6 +290,7 @@
<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="shareButton" destination="RmY-a3-hUg" id="Z54-ah-WAI"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="ZPN-tH-JAG" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
@ -292,6 +307,7 @@
<image name="multiply.circle.fill" catalog="system" width="64" height="60"/>
<image name="safari" catalog="system" width="64" height="60"/>
<image name="square.and.arrow.up" catalog="system" width="56" height="64"/>
<image name="square.and.arrow.up.fill" catalog="system" width="56" height="64"/>
<image name="star" catalog="system" width="64" height="58"/>
<namedColor name="primaryAccentColor">
<color red="0.031372549019607843" green="0.41568627450980394" blue="0.93333333333333335" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>

View File

@ -51,6 +51,8 @@
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Grant permission to save images from the article.</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>