forked from shadowfacts/Tusker
45 lines
1.4 KiB
Swift
45 lines
1.4 KiB
Swift
//
|
|
// ImageActivityItemSource.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 3/19/24.
|
|
// Copyright © 2024 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import UniformTypeIdentifiers
|
|
|
|
class ImageActivityItemSource: NSObject, UIActivityItemSource {
|
|
let data: Data
|
|
let url: URL
|
|
let image: UIImage?
|
|
|
|
init(data: Data, url: URL, image: UIImage?) {
|
|
self.data = data
|
|
self.url = url
|
|
self.image = image
|
|
}
|
|
|
|
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
|
|
return url
|
|
}
|
|
|
|
func activityViewController(_ activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: UIActivity.ActivityType?, suggestedSize size: CGSize) -> UIImage? {
|
|
return image
|
|
}
|
|
|
|
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
|
|
do {
|
|
let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent(url.lastPathComponent)
|
|
try data.write(to: tempURL)
|
|
return tempURL
|
|
} catch {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func activityViewController(_ activityViewController: UIActivityViewController, dataTypeIdentifierForActivityType activityType: UIActivity.ActivityType?) -> String {
|
|
return (UTType(filenameExtension: url.pathExtension) ?? .image).identifier
|
|
}
|
|
}
|