iOS Screen Capture View

iOS Screen Capture View

A UIView that records real-time video of its subviews and periodically samples the display to create a snapshot screen image. Can now add audio for global sounds and record full screen with or without status bar area. Get app video or device screen snapshots quickly. Includes full source code and a working demo.

UIScreenCaptureView, a UIView subclass that periodically samples its current display and stores it as a UIImage available through the currentScreen property. The sample/update rate can be configured (within reason) by setting the frameRate property.

Can now add audio for global sounds (app, mic, etc...) to the recording.

Can create a video of view, full screen with status bar or full screen without the status bar area. By default, the view and it's subviews are recorded. Get a screenshot at any time of a view, full screen with status area or full screen without status area.

By default, all screen snapshots are now done on a background thread to free up the UI main thread. A flag in UIScreenCaptureView class lets you switch between running snapshot on background or main thread.

This class can also be used to record real-time video of its subviews, using the startRecording and stopRecording methods. A new recording will overwrite any previously made recording file, so if you want to create multiple recordings per session (or across multiple sessions) then it is your responsibility to copy/back-up the recording output file after each session.

Some Uses

  • Create a video of your app
  • Create a video of a specific view in your app.
  • Create a video and include audio for global sounds such as app sounds, mic etc....
  • Create a video including full screen device area and status area.
  • Create a video including full screen device area and no status area.
  • Add screen capture video recording capability to any view in your app
  • Make the main view for your app to allow your app users to create videos on their own
  • Create screen snapshot images. Can access the individual screen capture images used to create the video.
  • Create screen snapshot. Periodically samples its current display to create a snapshot of the screen. So you do not have to even start recording to get screen snapshots. Use frameRate to adjust the sampling rate.
  • Create full-screen screen capture or video with or without the status area.
  • Get a screen capture of full screen with status area.
  • Get a screen capture of the full screen without status area.
  • Get a screen capture of a specific view.

New alternative methods that do not require adding UIScreenCaptureView as main view.
e.g.: [screenCaptureView startRecordingViewForTimePeriod:seconds view:self.view parent:self];

New static screenshot methods for just getting a screenshot image for an area of the screen.

Use UIScreenCaptureView like any UIView class and add views to record as subviews. If you would like to use the class to make a video of your app, make a UIScreenCaptureView the main view for your app. This way the recording will include all views in your app.

If you want to record a specific view only, then replace the view to record with a UIScreenCaptureView instance.

UIScreenCaptureView will make a recording of anything in it's subviews. Powerful and simple including audio, if desired.

It's tested and works fine on iOS7 but will work on any other version as well.

Hide

Supported OS & SDK Versions

  • ARC, iOS8+
  • Supported build target - iOS 6.0 and iOS7 / Mac OS 10.7 (Xcode 4.3.1, Apple LLVM compiler 3.1)
  • Earliest supported deployment target - iOS 6.0 / Mac OS 10.6
  • Earliest compatible deployment target - iOS 6.0 / Mac OS 10.6

NOTE: 'Supported' means that the library has been tested with this version. 'Compatible' means that the library should work on this OS version (i.e. it doesn't rely on any unavailable SDK features) but is no longer being tested for compatibility and may require tweaking or bug fixes to run correctly.

Hide

Installation

To install UIScreenCaptureView into your app, create a group named UIScreenCaptureView within your app project then drag the file from the ScreenCaptureView folder into the folder. Make sure that you indicate to copy the files into your project and set the target ownership of your app for all files copied.

You should copy these files: SystemUtilities.h, SystemUtiities.m, UIScreenCaptureView.h and UIScreenCaptureView.m

Hide

Usage

To use this class, you must link against the following frameworks:

  - AssetsLibrary
  - AVFoundation
  - CoreGraphics
  - CoreMedia
  - CoreVideo
  - QuartzCore
  - MediaPlayer

// see Demo Example in Examples/Demo folder for full example and source code

 // To use view to make a video of screen snapshots of you app.
 // First make your view the UIScreenCaptureView
 // then any view added to UIScreenCaptureView will included when recording…


  (void)viewDidLoad {
 [super viewDidLoad];

 // create screen capture view.   Any subviews added to screen capture view will be recorded.
 BOOL recordFullscreen = FALSE;    // turn on to capture fullscreen, otherwise view only
 BOOL recordStatusBar = FALSE;    // turn on to include status bar when capturing fullscreen
 BOOL recordAudio = FALSE;    // turn on to include audio for system sounds (app, mic, etc..)
 UIScreenCaptureView * screenCaptureView = [[UIScreenCaptureView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] fullscreen:recordFullscreen  includeStatusBar:recordStatusBar];
 [screenCaptureView includeAudio:recordAudio];  // indicates whether to include audio or not
 [screenCaptureView setVerbose:verbose];         // verbose debug mode on/off
 screenCaptureView.delegate = self;   // implements UIScreenCaptureViewDelegate interface
 self.view = screenCaptureView;

 // add your views to be recorded.   If you do not want to make the screenCaptureView your main view, see ALTERNATIVE below
 [self.view addSubView:yourView];

  // set some options
 [screenCaptureView setFrameRate:10.0f];            // 10 frames per seconds (10 frames is the default) adjust to control
                                                    // sampling of screen captures used in making video.
 [screenCaptureView setOutputName:@"output1.mp4"];  // output name (output.mp4 is the default)
 [screenCaptureView setVerbose:false];              // verbose debug mode (false is default)

  // ALTERNATIVE: if you did not want to set a UIScreenCaptureView as your parent view, then you
  // can use this alternate recording method to record your main views components.
  // To use this method you do not need to do the [self.view addSubview:yourView] call above
  // as you will be providing self.view which is your current view used.
  // [screenCaptureView startRecordingViewForTimePeriod:seconds view:self.view parent:self];

 // MANUAL RECORDING TIED TO BUTTONS if you want manual start recording by clicking a button, 
 // then create a UIButton that calls startRecording method when clicked.  Add another button 
 // to call stopRecording 

 // TIMED RECORDING: this test shows starting a timed recording for 20 seconds which records 
 // a video of view added to screenCaptureView
 [NSThread detachNewThreadSelector:@selector(startRecordingForTimePeriodTestThread:) toTarget:self withObject:@""];


 // can also use static methods to get a screen capture image
 //
 // fullscreen shot with no status bar
 UIImage * screenSnapshot = [UIScreenCaptureView takeScreenSnapshot];
 NSLog(@"screenSnapshot is width %.0f X height %.0f", screenSnapshot.size.width, screenSnapshot.size.height);
 // fullscreen shot with status bar
 UIImage * screenSnapshotWithStatusBar = [UIScreenCaptureView takeScreenSnapshotWithStatusBar];
 NSLog(@"screenSnapshot with status bar is width %.0f X height %.0f", screenSnapshotWithStatusBar.size.width, screenSnapshotWithStatusBar.size.height);
 // fullscreen shot with no status bar
 UIImage * screenSnapshotWithNoStatusBar = [UIScreenCaptureView takeScreenSnapshotWithNoStatusBar];
 NSLog(@"screenSnapshot with no status bar is width %.0f X height %.0f", screenSnapshotWithNoStatusBar.size.width, screenSnapshotWithNoStatusBar.size.height);

 // screenshot of a view...
 UIImage * screenSnapshotOfView = [UIScreenCaptureView takeScreenSnapshotOfView:self.view];
 NSLog(@"screenSnapshot of view is width %.0f X height %.0f", screenSnapshotOfView.size.width, screenSnapshotOfView.size.height);

 }

 // timed recording...
 (void)startRecordingForTimePeriodTestThread:(NSString *)arg {

[NSThread sleepForTimeInterval:1];
[self performSelectorOnMainThread:@selector(startRecordingForTimePeriodTest:) withObject:@"" waitUntilDone:NO];
}

 (void)startRecordingForTimePeriodTest:(NSString *)arg {

    [self startRecordingForTimePeriod:20];
}

 (void)startRecordingForTimePeriod:(int)seconds {

recordButton.hidden = TRUE;
stopButton.hidden = TRUE;

playIndex = 0;
recordingSeconds = seconds;
recordingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(recordingTimerElapsed:) userInfo:nil repeats:YES];   

[screenCaptureView setFrameRate:10.0f];             // 10 frames per seconds (10 frames is the default)
[screenCaptureView setOutputName:@"video.mp4"];     // output name (output.mp4 is the default)
[screenCaptureView setVerbose:FALSE];                // verbose debug mode off

// record any subview of screenCaptureView...
//[screenCaptureView startRecordingForTimePeriod:seconds];   // record for N seconds

// if you did not want to add a UIScreenCaptureView as your parent view, then you
// can use this alternate recording method to record your main views components.

// alternate method records any subview of self.view without having to 
// make screenCaptureView the parent view.   Make sure the view passed is the main app view...
// this is useful for adding app demo video recording to your app without modifying your view layout...
[screenCaptureView startRecordingViewForTimePeriod:seconds view:self.view parent:self];
 }

 (void)startRecording {

recordButton.hidden = TRUE;
stopButton.hidden = FALSE;

playIndex = 0;
recordingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(recordingTimerElapsed:) userInfo:nil repeats:YES];   

[screenCaptureView setFrameRate:10.0f];            // 10 frames per seconds (10 frames is the default)
[screenCaptureView setOutputName:@"video.mp4"];    // output name (output.mp4 is the default)
[screenCaptureView setVerbose:FALSE];              // verbose debug mode off
[screenCaptureView startRecording];                // verbose debug mode (false is default)
 }

// outputPathOrNil has the local output path if successful or nil if an error occurred...
 (void)recordingFinished:(NSURL *)outputPathOrNil {

 if (outputPathOrNil != nil) {
     NSLog(@"local output filename is %@", outputPathOrNil);

     // save the created video to Photos...
     NSString * outputName = [outputPathOrNil path];
     [screenCaptureView saveToPhotos:outputName target:self];

     // ---- other options:
     // play recorded video
     // [screenCaptureView playVideo:outputPathOrNil parent:self];

     // remove local copy
     // [screenCaptureView removeLocalCopyOfVideo:outputPathOrNil];    
     // ---- end other options
 } else
     NSLog(@"Error creating the video!");
}

 (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {

 if (error != nil)
     NSLog(@"Try to saved video and got the error: %@", [error description]);
}

// Constructors:

(id)initWithFrame:(CGRect)frame parent:(UIViewController *)parent;  
(id)initWithFrame:(CGRect)frame parent:(UIViewController *)parent fullscreen:(BOOL)_fullScreen includeStatusBar:(BOOL)includeStatusBar;

// Static Methods:

(UIImage *)takeScreenSnapshot;
(UIImage *)takeScreenSnapshotWithStatusBar;
(UIImage *)takeScreenSnapshotWithNoStatusBar;
(UIImage *)takeScreenSnapshotOfView:(UIView *)view;

// Methods:

(BOOL)startRecording;     // start recording screen captures...  
(BOOL)startRecordingForTimePeriod:(int)seconds;  // start recording screen captures for so many seconds...

 (BOOL)startRecordingView:(UIView *)view parent:(UIViewController *)_parent;     // start recording screen captures for a view...
 (BOOL)startRecordingViewForTimePeriod:(int)seconds view:(UIView *)view parent:(UIViewController *)_parent;  // start recording screen captures for so many seconds for a view...

 (void)stopRecording;   // stop recording screen captures and save video...

 (UIImage *)screenCaptureView:(UIView *)view;   // get screen capture image for a view

 (UIImage *)screenCaptureView2:(UIView *)view rect:(CGRect)rect;  // get screen capture image for a specific area of a view

 (UIImage *)getCurrentScreenshot;

 (UIImage *)getScreenshotForView:(UIView *)view;

 (UIImage *)getImageForView:(UIView *)view usePresentationLayer:(BOOL)usePresentationLayer;

 (void)playVideo:(NSURL *)videoUrl parent:(UIViewController *)parent;  // play a recorded view using the supplied view controller

 (BOOL)removeLocalCopyOfVideo:(NSURL *)localFile;    // remove the local copy of a recorded video

 (void)setVerbose:(BOOL)verbose;   // turn verbose mode on which displays more details when recording
Hide

Example Project Demo

The UIScreenCaptureViewDemo demo example in the Examples/Demo folder demonstrates how you might implement using UIScreenCaptureView.

The example is for iOS.

You need to log-in or create an account
  • Create an account
  • Log-in
Please use your real name.
Activation link will be sent to this address.
Minimum 8 characters
Enter your password again

Clicking this button confirms you read and agreed to the terms of use and privacy policy.

X

Save your watchlist

Fill your details below to receive project updates from your watch list - including new versions, price changes and discounts.

I agree to the terms of use and privacy policy.

3 licenses, starting from From » $39.99 14 day money-back guarantee View Licenses
or Get a quote

for customization or integration services

  • UIScreenCaptureView is very useful for me. And it saved my time.
    VG Vadim Glebov
    2 years ago, 0 comments
    Was this helpful?
    Flag
  • not so good,
    you delay with this code:
    [NSThread sleepForTimeInterval:delayRemaining > 0.0 ? delayRemaining : 0.01];

    but you forgot the duration for the creating of the screenshot. so the frame rate is not accurate.
    L liwei
    5 months ago, 0 comments
    Was this helpful?
    Flag
  • The code can be a little better with commenting and quality, I actually bought this for 29.99 now its 10.00.
    L Larry
    10 months ago, 0 comments
    Was this helpful?
    Flag
Post a comment

Or enter your name and Email
  • AR Ahmad Razoogi 2 weeks ago
    Does it support video & audio recording of a video player (UIView embedded)?
    • Thomas A. Markel Developer 2 days ago
      No the component will not record embedded video. If you try to include a video subview you will see a black area where the played video was. The component works by taking screenshots as close to the specified frame rate as possible and then assembling a video using the screenshots using the same frame rate.. Recording actual video is different and to merge the two methods is extremely difficult. Side note. there is an Undocumented screen snapshot call that exists in iOS that used to be public years and many versions ago. This undocumented API I assume is what Apple uses when you do a Sleep/Wake + Home to get a screen snapshot which will capture anything on the screen even when a video is playing. Apple almost took Sleep/Wake + Home screen capture method out then releasing iOS as it was initially only a developer tool. When Apple commented about plans to remove it, Beta testers (rumored was David Pogue) said please don't do that. Thank God they didn't as like would be a lot harder without it. It Apple would make the API public like would be grande. An easy way to capture video of an iOS device's screen is to run AirServer (http://www.airserver.com/) on your Mac or PC and then use AirPlay Mirroring to redirect your device's screen. The new AirServer version has added the ability to record a video of the AirServer session. It works great!
  • Thomas A. Markel Developer 2 weeks ago
    new version 4.3.1 supports ARC and iOS8+
  • AR Ahmad Razoogi 2 weeks ago
    Does it support iOS 8 ?