Fix saving images

This commit is contained in:
Justin Mazzocchi 2021-05-09 23:56:12 -07:00
parent f7ff1b8284
commit 99678e43fb
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C

View file

@ -193,9 +193,15 @@ extension ImageViewController {
}
func presentActivityViewController() {
if let image = imageView.image {
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)
let activityViewController = UIActivityViewController(
activityItems: [NSItemProvider(object: image)],
activityItems: [tempURL],
applicationActivities: [])
if UIDevice.current.userInterfaceIdiom == .pad {
@ -204,6 +210,9 @@ extension ImageViewController {
}
present(activityViewController, animated: true)
} catch {
alertUnableToExportMedia()
}
} else if let asset = playerView.player?.currentItem?.asset as? AVURLAsset {
asset.exportWithoutAudioTrack { result in
DispatchQueue.main.async {
@ -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()
}
}
}
@ -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)
}
}