简体   繁体   中英

Screen Capture in iOS 7

I'm working on a project and we would like to be able to record a UIView . The idea I had and implemented would be to take continuous screen shots and store them on disc and then after the UIView is done combine all the images to make a movie. Right now I'm using UIImage to combine and make a movie. This seems like a pretty slow process and I've read some things online, like RecordMyScreen project, but that uses private APIs like IOSurface and so on. I'm just wondering if theres a quick way to take screen shots. I would require to take about 25 a second. Right now I'm doing something like this:

UIGraphicsBeginImageContextWithOptions(self.captureView.frame.size, YES, 0.0);
// CGContextRef context = UIGraphicsGetCurrentContext();
// [self.captureView.layer renderInContext:context];
[self.captureView drawViewHierarchyInRect:weakSelf.captureView.bounds afterScreenUpdates:YES];
UIImage* screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"saving image");
[self saveImageToFolder:self.tempFolderPath image:screenShot];

this is in an NSOperationBlock to be performed in the background. Saving to disc is also done on another thread.

I know that AVAssetWriterInputPixelBufferAdaptor takes CVPixelBufferRef to create the movie so I'm not sure if theres a way to get that directly from the UIView ?

Anyway any help would be great!

Thanks

New API has been added since iOS 7, that should provide efficient way of getting snapshot

snapshotViewAfterScreenUpdates : renders the view into a UIView with unmodifiable content

resizableSnapshotViewFromRect:afterScreenUpdates:withCapInsets : same thing, but with resizable insets

drawViewHierarchyInRect:afterScreenUpdates : : same thing if you need all subviews to be drawn too (like labels, buttons...)

You can use the UIView returned for any UI effect, or render in into an image like you did if you need to export.

You can also try this, it worked for me.

- (UIImage *) screenshot {
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);

    [self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM