Skip to content

Commit

Permalink
优化代码结构
Browse files Browse the repository at this point in the history
  • Loading branch information
RickeyBoy committed Jun 21, 2024
1 parent c6427ec commit 4dcc274
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 116 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// CoreScreenshotExtensions.swift
//
//
// Created by Rickey on 2024/6/21.
//

import SwiftUI

extension UIView {
func takeScreenshot(afterScreenUpdates: Bool) -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { _ in
drawHierarchy(in: bounds, afterScreenUpdates: true)
}
}
}

extension View {
func takeScreenshot(frame:CGRect, afterScreenUpdates: Bool) -> UIImage {
let hosting = UIHostingController(rootView: self)
hosting.overrideUserInterfaceStyle = UIApplication.shared.currentUIWindow()?.overrideUserInterfaceStyle ?? .unspecified
hosting.view.frame = frame
hosting.ignoreSafeArea()
return hosting.view.takeScreenshot(afterScreenUpdates: afterScreenUpdates)
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
//
// ScreenshotableViewExtensions.swift
// ScreenshotableViewExample
// UtilExtensions.swift
//
// Created by Rickey on 2023/7/19.
//

import SwiftUI

extension UIView {
func takeScreenshot(afterScreenUpdates: Bool) -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { _ in
drawHierarchy(in: bounds, afterScreenUpdates: true)
}
}
}

extension View {
func takeScreenshot(frame:CGRect, afterScreenUpdates: Bool) -> UIImage {
let hosting = UIHostingController(rootView: self)
hosting.overrideUserInterfaceStyle = UIApplication.shared.currentUIWindow()?.overrideUserInterfaceStyle ?? .unspecified
hosting.view.frame = frame
hosting.view.backgroundColor = .yellow
return hosting.view.takeScreenshot(afterScreenUpdates: afterScreenUpdates)
}
}

extension UIHostingController {
func ignoreSafeArea() {
if #available(iOS 16.4, *) {
Expand Down
95 changes: 0 additions & 95 deletions Sources/ScreenshotableView/ScreenshotableView.swift

This file was deleted.

35 changes: 35 additions & 0 deletions Sources/ScreenshotableView/Views/ScreenshotableCotent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// SwiftUIView.swift
//
//
// Created by Rickey on 2024/6/21.
//

import SwiftUI

/// Normal View supports screenshot
struct ScreenshotableCotent<Content: View>: View {
@Binding var shotting: Bool
var completed: (UIImage) -> Void
let content: (_ style: ScreenshotableViewStyle) -> Content

public var body: some View {
func internalView(proxy: GeometryProxy) -> some View {
if self.shotting {
let frame = proxy.frame(in: .local)
DispatchQueue.main.async {
shotting = false
// 截图时用 inSnapshot 的样式
// Use content with '.inScreenshot' style while taking a screenshot
let screenshot = self.content(.inScreenshot).takeScreenshot(frame: frame, afterScreenUpdates: true)
self.completed(screenshot)
}
}
return Color.clear
}

// 展示 inView style 的内容
// Display content with 'inView' style
return content(.inView).background(GeometryReader(content: internalView(proxy:)))
}
}
55 changes: 55 additions & 0 deletions Sources/ScreenshotableView/Views/ScreenshotableViews.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// ScreenshotableView.swift
// ScreenshotableViewExample
//
// Created by Rickey on 2023/7/19.
//

import SwiftUI

public enum ScreenshotableViewStyle {
/// 截图中的样式
/// style in screenshot image
case inScreenshot
/// 正常展现的样式
/// style in normally displayed View
case inView
}

/// ScrollView supports screenshot
public struct ScreenshotableScrollView<Content: View>: View {
@Binding var shotting: Bool
var completed: (UIImage) -> Void
let content: (_ style: ScreenshotableViewStyle) -> Content

public init(shotting:Binding<Bool>, completed: @escaping (UIImage) -> Void, @ViewBuilder content: @escaping (_ style: ScreenshotableViewStyle) -> Content) {
self._shotting = shotting
self.completed = completed
self.content = content
}

public var body: some View {
ScrollView {
ScreenshotableCotent(shotting: $shotting, completed: completed, content: content)
}
}
}

/// ScrollView supports screenshot
public struct ScreenshotableView<Content: View>: View {
@Binding var shotting: Bool
var completed: (UIImage) -> Void
let content: (_ style: ScreenshotableViewStyle) -> Content

public init(shotting:Binding<Bool>, completed: @escaping (UIImage) -> Void, @ViewBuilder content: @escaping (_ style: ScreenshotableViewStyle) -> Content) {
self._shotting = shotting
self.completed = completed
self.content = content
}

public var body: some View {
ZStack {
ScreenshotableCotent(shotting: $shotting, completed: completed, content: content)
}
}
}

0 comments on commit 4dcc274

Please sign in to comment.