-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
118 additions
and
116 deletions.
There are no files selected for viewing
Binary file modified
BIN
+15.8 KB
(120%)
...eproj/project.xcworkspace/xcuserdata/timo.wang.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
27 changes: 27 additions & 0 deletions
27
Sources/ScreenshotableView/Extensions/CoreScreenshotExtensions.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
22 changes: 1 addition & 21 deletions
22
...leView/ScreenshotableViewExtensions.swift → ...tableView/Extensions/UtilExtensions.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
Sources/ScreenshotableView/Views/ScreenshotableCotent.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
55
Sources/ScreenshotableView/Views/ScreenshotableViews.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |