Swifty Codes
Swifty Codes

Reputation: 1122

view Recording in iOS swift?

Is there any library to record activity of a particular UIView ( I came across this lib [ASScreenRecorder-master] but its showing many errors when I tried to use it in swift ) or any other way to record the UIView which is displaying some animation like falling snow and slide show of the images? I want them to get recorded as a single video and to save them to the gallery. I have looked around for the some solutions but i am still unable to record the view. Please help. Thanks in advance.

func start() {
    let sharedRecorder = RPScreenRecorder.shared()

    // Do nothing if screen recording is not available
    guard sharedRecorder.isAvailable else { return }

    // Stop previous recording if necessary
    if sharedRecorder.isRecording {
        stopScreenRecording()
    }

    print("Starting screen recording")

    // Register as the recorder's delegate to handle errors.
    sharedRecorder.delegate = self

    // Start recording
    if #available(iOS 10.0, *) {
        #if os(iOS)
            sharedRecorder.isMicrophoneEnabled = true
            //sharedRecorder.isCameraEnabled = true // fixme
        #endif

        sharedRecorder.startRecording { [unowned self] error in
            if let error = error as? NSError, error.code != RPRecordingErrorCode.userDeclined.rawValue {
                print(error.localizedDescription)
                // Show alert
                return
            }
        }
    } else {
        // Fallback on earlier versions
        sharedRecorder.startRecording(withMicrophoneEnabled: true) { error in
            if let error = error as? NSError, error.code != RPRecordingErrorCode.userDeclined.rawValue {
                print(error.localizedDescription)
                // Show alert
                return
            }
        }
    }
}

Upvotes: 3

Views: 4415

Answers (1)

Andy Lebowitz
Andy Lebowitz

Reputation: 1511

Unfortunately, you cannot currently record a particular UIView. You can record the entire screen with ReplayKit, however I understand that was not what you were looking for.

Other ideas:

Record the "entire screen" but try to crop to the UIView. (see this tutorial). Think of a way in your app where it would make sense to record the entire screen- maybe expand the UIView to the screen boundaries.

Try looking at some of these Github files:

File1

File2

File3

Upvotes: 4

Related Questions