Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
Fix saving images
Browse files Browse the repository at this point in the history
  • Loading branch information
jzzocc committed May 10, 2021
1 parent f7ff1b8 commit 99678e4
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions View Controllers/ImageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,26 @@ extension ImageViewController {
}

func presentActivityViewController() {
if let image = imageView.image {
let activityViewController = UIActivityViewController(
activityItems: [NSItemProvider(object: image)],
applicationActivities: [])

if UIDevice.current.userInterfaceIdiom == .pad {
activityViewController.popoverPresentationController?
.barButtonItem = parent?.navigationItem.rightBarButtonItem
}
if let imageData = imageView.image?.sd_imageData(), let url = imageURL ?? viewModel?.attachment.url.url {
let tempURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent(url.lastPathComponent)

do {
try imageData.write(to: tempURL)

present(activityViewController, animated: true)
let activityViewController = UIActivityViewController(
activityItems: [tempURL],
applicationActivities: [])

if UIDevice.current.userInterfaceIdiom == .pad {
activityViewController.popoverPresentationController?
.barButtonItem = parent?.navigationItem.rightBarButtonItem
}

present(activityViewController, animated: true)
} catch {
alertUnableToExportMedia()
}
} else if let asset = playerView.player?.currentItem?.asset as? AVURLAsset {
asset.exportWithoutAudioTrack { result in
DispatchQueue.main.async {
Expand All @@ -224,18 +233,7 @@ extension ImageViewController {

self.present(activityViewController, animated: true)
case .failure:
let alertController = UIAlertController(
title: nil,
message: NSLocalizedString("attachment.unable-to-export-media", comment: ""),
preferredStyle: .alert)

let okAction = UIAlertAction(
title: NSLocalizedString("ok", comment: ""),
style: .default) { _ in }

alertController.addAction(okAction)

self.present(alertController, animated: true)
self.alertUnableToExportMedia()
}
}
}
Expand Down Expand Up @@ -291,4 +289,19 @@ private extension ImageViewController {
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
}
}

func alertUnableToExportMedia() {
let alertController = UIAlertController(
title: nil,
message: NSLocalizedString("attachment.unable-to-export-media", comment: ""),
preferredStyle: .alert)

let okAction = UIAlertAction(
title: NSLocalizedString("ok", comment: ""),
style: .default) { _ in }

alertController.addAction(okAction)

self.present(alertController, animated: true)
}
}

0 comments on commit 99678e4

Please sign in to comment.