Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update iOS orientation handling for application orientation and orientation changes #186

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jobs:
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4.2.2
- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4.1.0
with:
dotnet-version: '7.0.x'
- name: Setup NuGet 5.x
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
nuget sign .\artifacts\*.nupkg -CertificatePath $pfxPath -Timestamper http://timestamp.entrust.net/TSS/RFC3161sha2TS

- name: Artifacts
uses: actions/upload-artifact@v1
uses: actions/upload-artifact@v4.4.3
with:
name: NuGet
path: ./artifacts
Expand All @@ -64,11 +64,11 @@ jobs:
if: github.event_name == 'release'
steps:
- name: Download Artifacts
uses: actions/download-artifact@v1
uses: actions/download-artifact@v4.1.8
with:
name: NuGet
- name: Setup .NET Core
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4.1.0
with:
dotnet-version: '7.0.x'
- name: Push NuGet
Expand Down
46 changes: 36 additions & 10 deletions ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
AVCaptureVideoPreviewLayer videoPreviewLayer;
CaptureDelegate captureDelegate;
DispatchQueue dispatchQueue;
NSObject orientationObserver;
Dictionary<NSString, MSize> Resolutions => new()
{
{ AVCaptureSession.Preset352x288, new MSize(352, 288) },
Expand All @@ -43,11 +44,22 @@
videoPreviewLayer = new AVCaptureVideoPreviewLayer(captureSession);
videoPreviewLayer.VideoGravity = AVLayerVideoGravity.ResizeAspectFill;

// Observe device orientation changes to reapply layout subview
orientationObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIDevice.OrientationDidChangeNotification, OrientationChanged);

view = new PreviewView(videoPreviewLayer);

return view;
}

private void OrientationChanged(NSNotification notification)
{
if (view != null)
{
view.LayoutSubviews();
}
}

public void Connect()
{
UpdateCamera();
Expand Down Expand Up @@ -107,7 +119,7 @@
captureDevice = null;
}

var devices = AVCaptureDevice.DevicesWithMediaType(AVMediaTypes.Video.GetConstant());

Check warning on line 122 in ZXing.Net.MAUI/Apple/CameraManager.ios.maccatalyst.cs

View workflow job for this annotation

GitHub Actions / Build

This call site is reachable on: 'iOS' 18.0 and later, 'maccatalyst' 18.0 and later. 'AVCaptureDevice.DevicesWithMediaType(string)' is obsoleted on: 'ios' 10.0 and later (Use 'AVCaptureDeviceDiscoverySession' instead.), 'maccatalyst' 10.0 and later (Use 'AVCaptureDeviceDiscoverySession' instead.). (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1422)
foreach (var device in devices)
{
if (CameraLocation == CameraLocation.Front &&
Expand Down Expand Up @@ -161,6 +173,13 @@
captureDevice.Dispose();
captureDevice = null;
}

// Removing the orientationObserver
if (orientationObserver != null)
{
NSNotificationCenter.DefaultCenter.RemoveObserver(orientationObserver);
orientationObserver = null;
}
}
}

Expand All @@ -176,7 +195,7 @@
{
CaptureDevicePerformWithLockedConfiguration(() =>
captureDevice.TorchMode = on ? AVCaptureTorchMode.On : AVCaptureTorchMode.Off);
}
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -258,26 +277,33 @@
public override void LayoutSubviews()
{
base.LayoutSubviews();

// Determine the UIInterfaceOrientation based on the current interface (application) orientation instead of the device orientation
UIInterfaceOrientation uiInterfaceOrientation = (UIInterfaceOrientation)UIApplication.SharedApplication.ConnectedScenes.ToArray()
.Select(x => x as UIWindowScene)
.LastOrDefault(x => x?.Windows != null && x.Windows.Any(y => y.IsKeyWindow))?.InterfaceOrientation;

CATransform3D transform = CATransform3D.MakeRotation(0, 0, 0, 1.0f);
switch (UIDevice.CurrentDevice.Orientation)

switch (uiInterfaceOrientation)
{
case UIDeviceOrientation.Portrait:
case UIInterfaceOrientation.Portrait:
transform = CATransform3D.MakeRotation(0, 0, 0, 1.0f);
break;
case UIDeviceOrientation.PortraitUpsideDown:
transform = CATransform3D.MakeRotation((nfloat)Math.PI, 0, 0, 1.0f);
case UIInterfaceOrientation.PortraitUpsideDown:
transform = CATransform3D.MakeRotation((nfloat)(Math.PI), 0, 0, 1.0f);
break;
case UIDeviceOrientation.LandscapeLeft:
transform = CATransform3D.MakeRotation((nfloat)(-Math.PI / 2), 0, 0, 1.0f);
case UIInterfaceOrientation.LandscapeLeft:
transform = CATransform3D.MakeRotation((nfloat)(Math.PI / 2), 0, 0, 1.0f);
break;
case UIDeviceOrientation.LandscapeRight:
transform = CATransform3D.MakeRotation((nfloat)Math.PI / 2, 0, 0, 1.0f);
case UIInterfaceOrientation.LandscapeRight:
transform = CATransform3D.MakeRotation((nfloat)(-Math.PI / 2), 0, 0, 1.0f);
break;
}

PreviewLayer.Transform = transform;
PreviewLayer.Frame = Layer.Bounds;
}
}
}
}
#endif
Loading