Updates to latest release code
This commit is contained in:
parent
487b7938d9
commit
b69255df60
|
@ -3,17 +3,68 @@
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
public typealias PlatformImage = UIImage
|
public typealias PlatformImage = UIImage
|
||||||
|
public typealias PlatformScreen = UIScreen
|
||||||
|
|
||||||
internal typealias PlatformView = UIView
|
internal typealias PlatformView = UIView
|
||||||
internal typealias PlatformViewController = UIViewController
|
internal typealias PlatformViewController = UIViewController
|
||||||
|
|
||||||
|
extension UIScreen {
|
||||||
|
@nonobjc
|
||||||
|
public static var mainScreen: UIScreen {
|
||||||
|
return .main
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension UIImage {
|
||||||
|
public var png: Data? {
|
||||||
|
return self.pngData()
|
||||||
|
}
|
||||||
|
|
||||||
|
public func jpg(quality: CGFloat) -> Data? {
|
||||||
|
return self.jpegData(compressionQuality: quality)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#elseif os(macOS)
|
#elseif os(macOS)
|
||||||
|
|
||||||
import AppKit
|
import AppKit
|
||||||
|
|
||||||
public typealias PlatformImage = NSImage
|
public typealias PlatformImage = NSImage
|
||||||
|
public typealias PlatformScreen = NSScreen
|
||||||
|
|
||||||
internal typealias PlatformView = NSView
|
internal typealias PlatformView = NSView
|
||||||
internal typealias PlatformViewController = NSViewController
|
internal typealias PlatformViewController = NSViewController
|
||||||
|
|
||||||
|
extension NSScreen {
|
||||||
|
public static var mainScreen: NSScreen {
|
||||||
|
return NSScreen.main!
|
||||||
|
}
|
||||||
|
|
||||||
|
public var scale: CGFloat {
|
||||||
|
return backingScaleFactor
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension NSImage {
|
||||||
|
public var png: Data? {
|
||||||
|
return NSBitmapImageRep(data: tiffRepresentation!)?.representation(using: .png, properties: [:])
|
||||||
|
}
|
||||||
|
|
||||||
|
public func jpg(quality: CGFloat) -> Data? {
|
||||||
|
return NSBitmapImageRep(data: tiffRepresentation!)?.representation(using: .jpeg, properties: [.compressionFactor: quality])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
extension CGContext {
|
||||||
|
|
||||||
|
internal static var current: CGContext? {
|
||||||
|
#if os(OSX)
|
||||||
|
return NSGraphicsContext.current?.cgContext
|
||||||
|
#else
|
||||||
|
return UIGraphicsGetCurrentContext()
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ public final class ImageRenderer<Content>: ObservableObject where Content: View
|
||||||
public var content: Content
|
public var content: Content
|
||||||
public var label: String?
|
public var label: String?
|
||||||
public var proposedSize: ProposedViewSize = .unspecified
|
public var proposedSize: ProposedViewSize = .unspecified
|
||||||
public var scale: CGFloat = UIScreen.main.scale
|
public var scale: CGFloat = PlatformScreen.mainScreen.scale
|
||||||
public var isOpaque: Bool = false
|
public var isOpaque: Bool = false
|
||||||
public var colorMode: ColorRenderingMode = .nonLinear
|
public var colorMode: ColorRenderingMode = .nonLinear
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ public final class ImageRenderer<Content>: ObservableObject where Content: View
|
||||||
public extension ImageRenderer {
|
public extension ImageRenderer {
|
||||||
var cgImage: CGImage? {
|
var cgImage: CGImage? {
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
nsImage?.cgImage
|
nsImage?.cgImage(forProposedRect: nil, context: .current, hints: nil)
|
||||||
#else
|
#else
|
||||||
uiImage?.cgImage
|
uiImage?.cgImage
|
||||||
#endif
|
#endif
|
||||||
|
@ -31,7 +31,7 @@ public extension ImageRenderer {
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
|
|
||||||
var nsImage: NSImage? {
|
var nsImage: NSImage? {
|
||||||
nil
|
NSHostingController(rootView: content).view.snapshot
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
@ -44,7 +44,6 @@ public extension ImageRenderer {
|
||||||
|
|
||||||
let format = UIGraphicsImageRendererFormat(for: controller.traitCollection)
|
let format = UIGraphicsImageRendererFormat(for: controller.traitCollection)
|
||||||
format.opaque = isOpaque
|
format.opaque = isOpaque
|
||||||
format.preferredRange = colorMode.range
|
|
||||||
format.scale = scale
|
format.scale = scale
|
||||||
|
|
||||||
let renderer = UIGraphicsImageRenderer(size: size, format: format)
|
let renderer = UIGraphicsImageRenderer(size: size, format: format)
|
||||||
|
@ -62,8 +61,7 @@ public extension ImageRenderer {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#if os(macOS)
|
#if os(iOS)
|
||||||
#else
|
|
||||||
extension ColorRenderingMode {
|
extension ColorRenderingMode {
|
||||||
var range: UIGraphicsImageRendererFormat.Range {
|
var range: UIGraphicsImageRendererFormat.Range {
|
||||||
switch self {
|
switch self {
|
||||||
|
@ -74,3 +72,11 @@ extension ColorRenderingMode {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if os(macOS)
|
||||||
|
private extension NSView {
|
||||||
|
var snapshot: NSImage? {
|
||||||
|
return NSImage(data: dataWithPDF(inside: bounds))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
|
@ -13,7 +13,7 @@ extension View {
|
||||||
|
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
|
|
||||||
private struct ShareSheet: NSViewRepresentable where Data: RandomAccessCollection, Data.Element: Shareable {
|
private struct ShareSheet<Data>: NSViewRepresentable where Data: RandomAccessCollection, Data.Element: Shareable {
|
||||||
@Binding var item: ActivityItem<Data>?
|
@Binding var item: ActivityItem<Data>?
|
||||||
|
|
||||||
public func makeNSView(context: Context) -> SourceView {
|
public func makeNSView(context: Context) -> SourceView {
|
||||||
|
@ -27,7 +27,7 @@ private struct ShareSheet: NSViewRepresentable where Data: RandomAccessCollectio
|
||||||
final class SourceView: NSView, NSSharingServicePickerDelegate, NSSharingServiceDelegate {
|
final class SourceView: NSView, NSSharingServicePickerDelegate, NSSharingServiceDelegate {
|
||||||
var picker: NSSharingServicePicker?
|
var picker: NSSharingServicePicker?
|
||||||
|
|
||||||
var item: Binding<ActivityItem?> {
|
var item: Binding<ActivityItem<Data>?> {
|
||||||
didSet {
|
didSet {
|
||||||
updateControllerLifecycle(
|
updateControllerLifecycle(
|
||||||
from: oldValue.wrappedValue,
|
from: oldValue.wrappedValue,
|
||||||
|
@ -36,7 +36,7 @@ private struct ShareSheet: NSViewRepresentable where Data: RandomAccessCollectio
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
init(item: Binding<ActivityItem?>) {
|
init(item: Binding<ActivityItem<Data>?>) {
|
||||||
self.item = item
|
self.item = item
|
||||||
super.init(frame: .zero)
|
super.init(frame: .zero)
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ private struct ShareSheet: NSViewRepresentable where Data: RandomAccessCollectio
|
||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
private func updateControllerLifecycle(from oldValue: ActivityItem?, to newValue: ActivityItem?) {
|
private func updateControllerLifecycle(from oldValue: ActivityItem<Data>?, to newValue: ActivityItem<Data>?) {
|
||||||
switch (oldValue, newValue) {
|
switch (oldValue, newValue) {
|
||||||
case (.none, .some):
|
case (.none, .some):
|
||||||
presentController()
|
presentController()
|
||||||
|
@ -57,7 +57,7 @@ private struct ShareSheet: NSViewRepresentable where Data: RandomAccessCollectio
|
||||||
}
|
}
|
||||||
|
|
||||||
func presentController() {
|
func presentController() {
|
||||||
picker = NSSharingServicePicker(items: item.wrappedValue?.items ?? [])
|
picker = NSSharingServicePicker(items: item.wrappedValue?.data.map { $0 } ?? [])
|
||||||
picker?.delegate = self
|
picker?.delegate = self
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
guard self.window != nil else { return }
|
guard self.window != nil else { return }
|
||||||
|
@ -139,6 +139,7 @@ private extension ShareSheet {
|
||||||
let controller = UIActivityViewController(activityItems: item.wrappedValue?.data.map { $0 } ?? [], applicationActivities: nil)
|
let controller = UIActivityViewController(activityItems: item.wrappedValue?.data.map { $0 } ?? [], applicationActivities: nil)
|
||||||
controller.presentationController?.delegate = self
|
controller.presentationController?.delegate = self
|
||||||
controller.popoverPresentationController?.permittedArrowDirections = .any
|
controller.popoverPresentationController?.permittedArrowDirections = .any
|
||||||
|
controller.popoverPresentationController?.sourceRect = view.bounds
|
||||||
controller.popoverPresentationController?.sourceView = view
|
controller.popoverPresentationController?.sourceView = view
|
||||||
controller.completionWithItemsHandler = { [weak self] _, _, _, _ in
|
controller.completionWithItemsHandler = { [weak self] _, _, _, _ in
|
||||||
self?.item.wrappedValue = nil
|
self?.item.wrappedValue = nil
|
||||||
|
|
|
@ -45,7 +45,13 @@ extension Image: Shareable {
|
||||||
.appendingPathComponent("\(UUID().uuidString)")
|
.appendingPathComponent("\(UUID().uuidString)")
|
||||||
.appendingPathExtension(pathExtension)
|
.appendingPathExtension(pathExtension)
|
||||||
let renderer = ImageRenderer(content: self)
|
let renderer = ImageRenderer(content: self)
|
||||||
|
|
||||||
|
#if os(iOS)
|
||||||
let data = renderer.uiImage?.jpegData(compressionQuality: 0.8)
|
let data = renderer.uiImage?.jpegData(compressionQuality: 0.8)
|
||||||
|
#else
|
||||||
|
let data = renderer.nsImage?.jpg(quality: 0.8)
|
||||||
|
#endif
|
||||||
|
|
||||||
try data?.write(to: url, options: .atomic)
|
try data?.write(to: url, options: .atomic)
|
||||||
return .init(contentsOf: url)
|
return .init(contentsOf: url)
|
||||||
} catch {
|
} catch {
|
||||||
|
@ -61,7 +67,7 @@ extension PlatformImage: Shareable {
|
||||||
let url = URL(fileURLWithPath: NSTemporaryDirectory())
|
let url = URL(fileURLWithPath: NSTemporaryDirectory())
|
||||||
.appendingPathComponent("\(UUID().uuidString)")
|
.appendingPathComponent("\(UUID().uuidString)")
|
||||||
.appendingPathExtension(pathExtension)
|
.appendingPathExtension(pathExtension)
|
||||||
let data = jpegData(compressionQuality: 0.8)
|
let data = jpg(quality: 0.8)
|
||||||
try data?.write(to: url, options: .atomic)
|
try data?.write(to: url, options: .atomic)
|
||||||
return .init(contentsOf: url)
|
return .init(contentsOf: url)
|
||||||
} catch {
|
} catch {
|
||||||
|
|
Loading…
Reference in New Issue