Save GIFs Locally
This commit is contained in:
@@ -33,55 +33,47 @@ class MessagesViewController: MSMessagesAppViewController {
|
||||
}
|
||||
|
||||
private func sendGIF(_ gif: GIF) {
|
||||
guard let conversation = activeConversation,
|
||||
let gifURL = gif.url
|
||||
else { return }
|
||||
|
||||
// Show a loading indicator
|
||||
let loadingAlert = UIAlertController(
|
||||
title: "Preparing GIF", message: "Please wait...", preferredStyle: .alert)
|
||||
present(loadingAlert, animated: true)
|
||||
|
||||
// Download the GIF data
|
||||
GIFDownloadService.shared.downloadGIF(from: gif.urlString) { data, error in
|
||||
DispatchQueue.main.async {
|
||||
// Dismiss the loading indicator
|
||||
self.dismiss(animated: true) {
|
||||
if let error = error {
|
||||
self.showErrorAlert(error: error)
|
||||
return
|
||||
}
|
||||
|
||||
guard let gifData = data else {
|
||||
self.showErrorAlert(message: "Failed to download GIF")
|
||||
return
|
||||
}
|
||||
|
||||
// Create a temporary file URL for the GIF
|
||||
let tempDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
|
||||
let tempFileURL = tempDirectoryURL.appendingPathComponent(UUID().uuidString)
|
||||
.appendingPathExtension("gif")
|
||||
|
||||
do {
|
||||
// Write GIF data to temporary file
|
||||
try gifData.write(to: tempFileURL)
|
||||
|
||||
// Insert the GIF directly as a standard attachment into the message field
|
||||
conversation.insertAttachment(tempFileURL, withAlternateFilename: "animated.gif") {
|
||||
error in
|
||||
if let error = error {
|
||||
self.showErrorAlert(error: error)
|
||||
} else {
|
||||
// Successfully inserted the attachment
|
||||
self.requestPresentationStyle(.compact)
|
||||
guard let conversation = activeConversation else { return }
|
||||
|
||||
// Show a loading indicator
|
||||
let loadingAlert = UIAlertController(title: "Preparing GIF", message: "Please wait...", preferredStyle: .alert)
|
||||
present(loadingAlert, animated: true)
|
||||
|
||||
// Load the GIF data from local storage
|
||||
DispatchQueue.global(qos: .userInitiated).async {
|
||||
let gifData = GIFStorageService.shared.getGIFData(for: gif)
|
||||
|
||||
DispatchQueue.main.async {
|
||||
// Dismiss the loading indicator
|
||||
self.dismiss(animated: true) {
|
||||
guard let gifData = gifData else {
|
||||
self.showErrorAlert(message: "Failed to load GIF from storage")
|
||||
return
|
||||
}
|
||||
|
||||
// Create a temporary file URL for the GIF
|
||||
let tempDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
|
||||
let tempFileURL = tempDirectoryURL.appendingPathComponent(UUID().uuidString).appendingPathExtension("gif")
|
||||
|
||||
do {
|
||||
// Write GIF data to temporary file
|
||||
try gifData.write(to: tempFileURL)
|
||||
|
||||
// Insert the GIF directly as a standard attachment into the message field
|
||||
conversation.insertAttachment(tempFileURL, withAlternateFilename: "animated.gif") { error in
|
||||
if let error = error {
|
||||
self.showErrorAlert(error: error)
|
||||
} else {
|
||||
// Successfully inserted the attachment
|
||||
self.requestPresentationStyle(.compact)
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
self.showErrorAlert(error: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
self.showErrorAlert(error: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func showErrorAlert(error: Error? = nil, message: String? = nil) {
|
||||
|
||||
Reference in New Issue
Block a user