iOS: Scale camera frames to the requested size instead of cropping

The frame was drawn at its own size into a context of the size the game
asked for, so the game got one corner of the picture. Also drop the
preview layer, which was never shown.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Opus 5.5 committed 2026-10-10 16:28:27 +02:00
1 parent b4775cde07
commit aee93405f5
1 file changed
+2 -7
+2 -7
View File
@@ -6,7 +6,6 @@
@interface CameraHelper() {
AVCaptureSession *captureSession;
AVCaptureVideoPreviewLayer *previewLayer;
int mWidth;
int mHeight;
}
@@ -117,11 +116,6 @@ NSString *getSelectedCamera() {
dispatch_queue_t queue = dispatch_queue_create("cameraQueue", NULL);
[videoOutput setSampleBufferDelegate:self queue:queue];
previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession];
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[previewLayer setFrame:CGRectMake(0, 0, mWidth, mHeight)];
[captureSession startRunning];
});
}
@@ -156,7 +150,8 @@ NSString *getSelectedCamera() {
CGImageRef inImage = CGBitmapContextCreateImage(inContext);
CGContextRelease(inContext);
CGRect outRect = CGRectMake(0, 0, width, height);
// Scale the frame to the size the game asked for.
CGRect outRect = CGRectMake(0, 0, mWidth, mHeight);
CGContextRef outContext = CGBitmapContextCreate(nil, mWidth, mHeight, 8, mWidth * 4, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(outContext, outRect, inImage);
CGImageRelease(inImage);