All Projects → frolovilya → SwiftUIViewRecorder

frolovilya / SwiftUIViewRecorder

Licence: MIT license
Efficiently record any SwiftUI View as image or video

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to SwiftUIViewRecorder

MultiModal
Use multiple .sheet, .alert, etc. modifiers in the same SwiftUI View
Stars: ✭ 49 (+145%)
Mutual labels:  view, swiftui
Observable
A generic ObservableObject for every property!
Stars: ✭ 41 (+105%)
Mutual labels:  view, swiftui
TabBar
📱 TabBar – highly customizable tab bar for your SwiftUI application.
Stars: ✭ 105 (+425%)
Mutual labels:  view, swiftui
SignatureView
【Android View】:好用的Android电子签名板,能保存所签名的图片
Stars: ✭ 89 (+345%)
Mutual labels:  view
SPConfetti
Show the confetti only when the user is having fun, and if not having fun, don't show it.
Stars: ✭ 187 (+835%)
Mutual labels:  view
Shift
Light-weight EventKit wrapper.
Stars: ✭ 31 (+55%)
Mutual labels:  swiftui
SwiftUI-MVVM-C
An iOS template project using SwiftUI, Combine and MVVM-C software architecture
Stars: ✭ 85 (+325%)
Mutual labels:  swiftui
aprenda-swift
Uma lista de conteúdos para você aprender Swift
Stars: ✭ 429 (+2045%)
Mutual labels:  swiftui
OnlySwitch
⚙️ All-in-One menu bar app, hide 💻MacBook Pro's notch, dark mode, AirPods, Shortcuts
Stars: ✭ 1,288 (+6340%)
Mutual labels:  swiftui
AppStore-Connect-Widget
Open source AppStore sales monitor with widget
Stars: ✭ 36 (+80%)
Mutual labels:  swiftui
TextBanner
搜索栏文字轮播切换控件,京东淘宝头条资讯轮播
Stars: ✭ 35 (+75%)
Mutual labels:  view
moestreamer
macOS menubar music player
Stars: ✭ 17 (-15%)
Mutual labels:  swiftui
brain-marks
Save and categorize tweets
Stars: ✭ 47 (+135%)
Mutual labels:  swiftui
TicTacToe
No description or website provided.
Stars: ✭ 40 (+100%)
Mutual labels:  view
BarChart
SwiftUI Bar Chart
Stars: ✭ 156 (+680%)
Mutual labels:  swiftui
SwiftUI-Currency-Converter
Currency Converter project coded by SwiftUI and Swift5
Stars: ✭ 56 (+180%)
Mutual labels:  swiftui
android-prefix-suffix-edit-text
EditText with support for non editable prefix and suffix.
Stars: ✭ 36 (+80%)
Mutual labels:  view
react-native-masonry-brick-list
Staggered Or Masonary List View For React Native Written in pure js
Stars: ✭ 24 (+20%)
Mutual labels:  view
Chat
A basic SwiftUI chat app that leverages the new URLSessionWebSocketTask.
Stars: ✭ 22 (+10%)
Mutual labels:  swiftui
SwiftUI-Tutorial-Playground
A Playground for running SwiftUI tutorial code on macOS Mojave
Stars: ✭ 29 (+45%)
Mutual labels:  swiftui

SwiftUIViewRecorder

Package to efficiently record any SwiftUI View as image or video.

Requirements

  • iOS 13.0
  • Swift 5.2

Installation

Use Xcode's built-in Swift Package Manager:

Usage

Record View animation as a .mov QuickTime video

func recordVideo(duration: Double? = nil,
                 framesPerSecond: Double = 24,
                 useSnapshots: Bool = false) throws -> ViewRecordingSession<URL>

Note that each SwiftUI View is a struct, thus it's copied on every assignment. Video capturing happens off-screen on a view's copy and intended for animation to video conversion rather than live screen recording.

Recording performance is much better when setting useSnapshots to true. In this case view frame capturing and image rendering phases are separated, but the feature is only available on a simulator due to security limitations. Use snapshotting when you need to record high-FPS animation on a simulator to render it as a video.

You could use provided ViewRecordingSessionViewModel or write your own recording session view model to handle recording progress.

import SwiftUI
import SwiftUIViewRecorder

struct MyViewWithAnimation: View {
    
    // observe changes using built-in recording view model
    @ObservedObject var recordingViewModel: ViewRecordingSessionViewModel<URL>
    
    private var viewToRecord: some View {
        // some view with animation which we'd like to record as a video
    }
    
    var body: some View {
        ZStack {
            if (recordingViewModel.asset != nil) {
                Text("Video URL \(recordingViewModel.asset!)")
            } else {
                Text("Recording video...")
            }
        }
        .onAppear {
            recordingViewModel.handleRecording(session: try! viewToRecord.recordVideo())
        }
    }
    
}

Snapshot View as UIImage

func asImage() -> Future<UIImage, Never>

Simply call asImage() on any View to capture it's current state as an image. Since image is returned as a Future value, view model must be used to handle the result. You could use provided ViewImageCapturingViewModel or write your own.

import SwiftUI
import SwiftUIViewRecorder

struct ContentView: View {

    // observe image generation using built-in recording view model
    @ObservedObject var imageCapturingViewModel: ViewImageCapturingViewModel

    var viewToImage: some View {
        Circle()
            .fill(Color.yellow)
            .frame(width: 50, height: 50)
            .asImage()
    }
    
    var body: some View {
        ZStack {
            if (imageCapturingViewModel.image != nil) {
                Image(uiImage: imageCapturingViewModel.image!)
                    .resizable()
                    .scaledToFit()
                    .frame(width: 200, height: 200)
            }
        }
        .onAppear {
            imageCapturingViewModel.captureImage(view: viewToImage)
        }
    }

}

Record view with a custom frames renderer

It's possible to impement and use your own FramesRenderer to convert array of UIImage frames to some asset.

First, implement FramesRenderer protocol:

class CustomRenderer: FramesRenderer {
    func render(frames: [UIImage], framesPerSecond: Double) -> Future<CustomAsset?, Error> {
        // process frames to a CustomAsset
    }
}

Second, init ViewRecordingSession with your custom renderer:

extension SwiftUI.View {
    public func toCustomAsset(duration: Double? = nil,
                              framesPerSecond: Double = 24) throws -> ViewRecordingSession<CustomAsset> {
        try ViewRecordingSession(view: self,
                                 framesRenderer: CustomRenderer(),
                                 duration: duration,
                                 framesPerSecond: framesPerSecond)
    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].