How do you screenshot an ARKit camera view? - ios

Looking for a way to capture a screenshot or video of the ARScene without the buttons and other subviews
Traditional screen capture captures everything visible on the screen including UI controls

ARSCNViews have a built in snapshot method that returns a UIImage of the ARScene
So in Objective-C you could do:
UIImage *image = [sceneView snapshot];
and in Swift
var image = sceneView.snapshot()

Related

swift: avfoundation to capture images

i basically have two UIImages. One called previewImage that displays what the AVFoundation Camera displays, and captureImage that displays the image taken using the camera. When I had:
previewLayer!.videoGravity = AVLayerVideoGravityResize
the two images displayed the same formatting, but when I changed it to:
previewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
it seems that the previewImage is horizontally flattened. Is there a way to adjust the captureImage UIImage so that it displays what the previewImage displays through the video feed?Part of code that assigns image to imageView UIImage

Screenshot programmatically and blur in Swift

I want to take a screenshot from my app programmatically and the code working fine but I have a UIVisualEffectView with blur effect and the screenshot gave me the image without blur!
How I can make the screenshot take the blur also?
UIGraphicsBeginImageContextWithOptions(fullView.bounds.size, true, 1)
self.fullView.layer.renderInContext(UIGraphicsGetCurrentContext())
var viewImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil)
To retain the UIVisualEffectView Blur when taking an image of the screen programmatically, you must be taking an image of the entire screen.
Here is a more technical definition provided by Apple:
Many effects require support from the window that hosts the
UIVisualEffectView. Attempting to take a snapshot of only the
UIVisualEffectView will result in a snapshot that does not contain the
effect. To take a snapshot of a view hierarchy that contains a
UIVisualEffectView, you must take a snapshot of the entire UIWindow or
UIScreen that contains it. - Apple Documentation
After taking a screenshot, a blur effect can be added programmatically. Here's an example of using the GPUImage library, which can be installed using CocoaPods. A similar effect can also be achieved using native Apple libraries.
GPUImagePicture *picture = [[GPUImagePicture alloc] initWithImage:snapShot];
GPUImageiOSBlurFilter *blurFilter = [[GPUImageiOSBlurFilter alloc] init];
[blurFilter setBlurRadiusInPixels:4];
[picture addTarget:blurFilter];
[blurFilter useNextFrameForImageCapture];
[picture processImage];
UIImage *processed = [blurFilter imageFromCurrentFramebuffer];
I had this same problem. I had a view controller with a visual effect (blur) applied to a background image and some other non-blurred view on top and I wanted to take a screenshot of that controller.
As highlighted by #user3721428 trying to capture the view doesn't work. For my surprise trying to render the window layer in context doesn't work.
What worked is to get a snapshotView from UIScreen:
let view = UIScreen.main.snapshotView(afterScreenUpdates: true)
And then grab an image from that view using a method like in #neave answer here

need a very tiny (rectangular in shape) overlay over UIImagePickerController, and then crop the image accordingly - UPDATED

In my application, i need the user to take a snap of only a 10 letter word (using overlay, which should be right in the centre of the screen of the UIImagePicker), and then in need to show him that image (only the part of the image covered by that rectangle). So, I need to crop that image according to the overlay.
Here, i have taken a picture using UIImagePickerControl. Now, i want to see the dimensions of the image that i have taken..
UIImage *imageToprocess = [info objectForKey:UIImagePickerControllerOriginalImage];
NSLog(#"image width %f", imageToprocess.size.width);
NSLog(#"image height %f", imageToprocess.size.height);
I see the following result on console.. But how is this possible. the dimensions of the image is exceeding the dimension of the iPhone screen size.. (which is 320, 568)
UsingTesseractOCR[524:60b] image width 2448.000000
2013-12-17 16:02:18.962 UsingTesseractOCR[524:60b] image height 3264.000000
Can anybody help me out here?? I have gone through several questions here, but did not understand how to do it.
Please help..
Refer this sample code for image capturing and cropping.
https://github.com/kishikawakatsumi/CropImageSample
For creating overlay, first create a custom view (of full dimensions of camera preview) and add an transparent image with just a rectangle in its background. use this view as overlay view.
myview =[[UIImageView alloc]init];
myview.frame=CGRectMake(0, 0, 320, 431);
// why 431? bcoz height = height of device - height of tabbar present in the
bottom for camera controls of picker
//for iphone 4 ,480-49
myview.backgroundColor =[UIColor clearColor];
myview.opaque = NO;
myview.image =[UIImage imageNamed:#"A45Box.png"];
myview.userInteractionEnabled =YES;
note that you create a background image appropriately (means dimensions). You can also draw rectangle programmatically but this is much easy way.
Secondly, talking about your cropping issue, you have to get your hands dirty....Try these links for help
https://github.com/iosdeveloper/ImageCropper
https://github.com/barrettj/BJImageCropper
https://github.com/ardalahmet/SSPhotoCropperViewController

iPhone: Capture iOS Camera with Overlay View

In my application i am capturing picture through camera with overlay view & in the overlay view, there is a custom button through which i want to capture the whole screen.Overlay view is transparent at some points where i want to capture image. I am doing it like this:
- (IBAction)captue:(id)sender
{
[self setBackgroundColor:[UIColor clearColor]];
UIGraphicsBeginImageContext(self.frame.size);
[self.layer.presentationLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
It is capturing the image of overlay view but at the camera view (the view where overlay is transparent and i want to show camera view there) it is capturing black color instead of a photo. Anyone please tell me am i doing something wrong?
I found Screen capture is the one of the thing to capture camera view with overlay. But i didn't get the preview layer in screen captured video(in case video recording). Look at MyAVControllerDemo code to get clear idea and i used IAScreenCaptureView to capture video, or simple snap shot. Now working properly.
Using AVFoundationFramework to fix your problem.
Referred this link: http://code4app.net/ios/Camera-With-AVFoundation/5003cb1d6803fa9a2c000000

iOS UIImage stretching: How to fit a larger UIImage in a smaller UIImageView without any quality decrease and without any spaces

![enter image description here][1]I am taking a UIImage from UIImagePickerController and showing it in a UIImageView. Actually the UIImagePickerController is in full screen and thus the UIImage is also. But UIImageView is not in full screen because of presence of a header bar.
The Image is getting stretched..
How do I fix this..
Please help me someone..
THANKS IN ADVANCE.....
First image is the image picker screen and second one is where I am displaying the UIImage captured from picker in a UIImageView.
![This is my UIImagePickerController][2]
![This is the image after capturing being shown in a UIImageview. ][3]
NOTE: BOTH THE SCREENS ARE IN LANDSCAPE MODE
Use this for your UIImageView
imageView.contentMode = UIViewContentModeScaleAspectFill;
You won't get any space and with scale preserved. However, some part of the image will be clipped off.
If you use:
imageView.contentMode = UIViewContentModeScaleAspectFit;
There will be some empty space, but scale is preserved.

Resources