swift: avfoundation to capture images - ios

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

Related

camera roll image is bigger than the camera preview

I am creating a camera app. I can take a image and the image is passing back to a view controller to show the image. It also saved to the camera roll.
If I compare the image with the camera preview to the saved image, it seems like that the camera preview is a little bit zoomed in.
Image from camera preview:
Image from Gallery:
This is my code so far from the camera preview:
let cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewLayer.videoGravity = .resizeAspectFill
cameraPreviewLayer.connection?.videoOrientation = .portrait
cameraPreviewLayer.frame = view.frame
view.layer.insertSublayer(cameraPreviewLayer, at: 0)
captureSession.startRunning()
How can I make the camera preview to the same size as the saved image?
The culprit is the videoGravity setting. .resizeAspectFill will resize the preview to fill the whole view, even when that means cropping content.
If you want to see the whole frame, set it to .resizeAspect. This will introduce black bars, though.
If you want the captured Image to have the same size as the camera preview. You should set videoGravity = .resize

How to display image overlay on video in external playback mode of AVPlayer via AirPlay?

How to add an overlay UIImage in PNG format (such as a logo) with alpha channel on top of video playback, especially in case of external playback mode of AVPlayer when video is casted on Apple TV via AirPlay?
I would like add an overlay UIImage in PNG format with alpha channel (such as a logo) on top of video playback. This can be easily done on the phone by using contentOverlayView of AVPlayerViewController. However, when the video is casted and played on Apple TV via AirPlay, the contentOverlayView does not display.
I also tried to apply customised UIImageView on external screen when UIScreen has detected the external screen. However, the image still does not show. Instead, only the video playback is shown on the external screen. Here you can see my code for this approach:
if UIScreen.screens.count > 1 {
let externalScreen = UIScreen.screens[1]
print("Playing: externalScreen.bounds: \(externalScreen.bounds)")
let secondWindow = UIWindow(frame: externalScreen.bounds)
secondWindow.screen = externalScreen
let overlayImage = UIImage(named: "rain.png")
let overlayImageView = UIImageView(frame: externalScreen.bounds)
overlayImageView.image = overlayImage
secondWindow.addSubview(overlayImageView)
secondWindow.isHidden = false
secondWindow.makeKeyAndVisible()
}
It does not sound like a difficult problem as adding overlay views like subtitle or image logo seems to be very common operations. Can somebody help?
Thanks.

SceneKit UIImage material is black

I'm loading images from the photo library via UIImagePickerController and with that image I'm setting the material of a SCNPlane:
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFit
plane.firstMaterial?.diffuse.contents = imageView
With some images, this works fine, no problem at all, the image shows up properly. However, with other images, the texture of the plane shows up as entirely black and Xcode prints "Unsupported IOSurface format: 0x00000000". It seems to be only images that were screenshots causing this, although some screenshot images work just fine.
SceneKit can display both PNGs and JPGs, some PNGs just seem to cause issues. Not sure why or whether it's a bug or not. What you can do that's probably better than converting everything to JPGs (so you can retain transparency) is set your material contents to the image's CGImage:
if let cgImage = image.cgImage {
geometry.firstMaterial?.diffuse.contents = cgImage
}
You can also convert your image to a JPEG via something like this, as SceneKit doesn't seem to have issues with JPGs:
if let jpegData = image.jpegData(compressionQuality: 1.0) {
image = UIImage(data: jpegData)!
}
You will lose transparency doing it this way however.

How do you screenshot an ARKit camera view?

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()

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.

Categories

Resources