Repository: flutter
Issue: iOS: UIGraphicsImageRenderer can't render FlutterView layer
Labels: platform-ios,engine,has reproducible steps,P2,team-ios,triaged-ios,found in release: 3.16,found in release: 3.20

### Steps to reproduce

1. Run Flutter application on iOS simulator/device
2. Get reference to layer of most top [FlutterView](https://api.flutter.dev/ios-embedder/interface_flutter_view.html)
3. Create `UIGraphicsImageRenderer` based on that layer
4. Use render to create UIImage.

### Expected results

- UIImage has the proper size and reflects the FlutterView layer
- No Xcode warning about CGBitmapContextGetHeight

### Actual results

1. Xcode prints a warning message 
```console
CGBitmapContextGetHeight: invalid context 0x600003704e40. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
```
Reason: https://api.flutter.dev/ios-embedder/_flutter_view_8mm_source.html line 183

2. `UIGraphicsImageRenderer` returns empty UIImage instance

NB! The same mechanism works well with regular UIView.

### Code sample

<details open><summary>Code sample</summary>

```swift
import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {

        GeneratedPluginRegistrant.register(with: self)

        // Waits for layouting UI.
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3)) {
            if let layer = UIApplication.shared.windows.first?.rootViewController?.view.layer {
                let renderer = UIGraphicsImageRenderer(size: layer.frame.size)
                let image = renderer.image { context in
                    layer.render(in: context.cgContext)
                }
                print("Empty/black image: ", image)
            }
        }

        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}
```

[ios_issue.zip](https://github.com/flutter/flutter/files/14271610/ios_issue.zip)

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

![image](https://github.com/flutter/flutter/assets/28916962/c940bba1-3d40-4c77-a50e-46755f6e7a20)


</details>


### Logs

<details open><summary>Logs</summary>

Flutter output
```console
Launching lib/main.dart on iPhone 15 in debug mode...
main.dart:1
Xcode build done.                                           29.8s
[VERBOSE-2:FlutterDarwinContextMetalImpeller.mm(35)] Using the Impeller rendering backend.
Connecting to VM Service at ws://127.0.0.1:53066/flsCttNRM44=/ws
```

Xcode
```console
[VERBOSE-2:FlutterDarwinContextMetalImpeller.mm(35)] Using the Impeller rendering backend.
flutter: The Dart VM service is listening on http://127.0.0.1:52515/6x-427nmrM0=/
CGBitmapContextGetHeight: invalid context 0x600003774240. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Empty/black image:  <UIImage:0x600003012a30 anonymous {393, 852} renderingMode=automatic(original)>
```
</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.10.5, on macOS 14.3.1 23D60 darwin-x64, locale en-GB)
    • Flutter version 3.10.5 on channel stable at /Users/%USERNAME%/Developer/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 796c8ef792 (8 months ago), 2023-06-13 15:51:02 -0700
    • Engine revision 45f6e00911
    • Dart version 3.0.5
    • DevTools version 2.23.1

[!] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /Users/%USERNAME%/Library/Android/sdk
    • Platform android-33, build-tools 32.1.0-rc1
    • ANDROID_HOME = /Users/%USERNAME%/Library/Android/sdk
    • ANDROID_SDK_ROOT = /Users/%USERNAME%/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] VS Code (version 1.86.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.82.0

[✓] Connected device (5 available)
    • Yurii’s iPhone (mobile)  • 00008020-001C35EE1A52002E            • ios            • iOS 16.7.2 20H115
    • iPhone 12 (RED) (mobile) • 00008101-001168461E10001E            • ios            • iOS 17.2.1 21C66
    • iPhone 15 (mobile)       • A412B6FF-467D-49B5-A98B-19C31CA29A63 • ios            •
      com.apple.CoreSimulator.SimRuntime.iOS-17-2 (simulator)
    • macOS (desktop)          • macos                                • darwin-x64     • macOS 14.3.1 23D60 darwin-x64
    • Chrome (web)             • chrome                               • web-javascript • Google Chrome 121.0.6167.160

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.
```

</details>