Skip to content

feat(view-shot): render options, image disposal and crash fixes - #25

Open
edusperoni wants to merge 3 commits into
masterfrom
feat/view-shot-render-options
Open

edusperoni wants to merge 3 commits into
masterfrom
feat/view-shot-render-options

Conversation

@edusperoni

@edusperoni edusperoni commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Improvements to @valor/nativescript-view-shot from profiling an app that snapshots offscreen views many times per second. Intended for a minor release.

Crash fixes

  • iOS: render with UIGraphicsImageRenderer instead of the deprecated (iOS 17) UIGraphicsBeginImageContextWithOptions. The old API raises an uncatchable NSInternalInconsistencyException on a 0×0 size or a failed bitmap allocation, and leaks its context if rendering throws. No version gate is needed: the renderer exists since iOS 10 and preferredRange since iOS 12.
  • Both platforms: renderToImageSource returns null for views with no native view or an empty size (typically a missing measureAndLayout) instead of crashing. On Android, Bitmap.createBitmap(0, …) used to throw.
  • Android: background views attach to foregroundActivity ?? startActivity instead of always startActivity.
  • Android: disposeBackgroundView no longer throws when the view is already detached.
  • Angular: captureInBackground disposes the background views and destroys the Angular view in finally, so a throwing delay callback or render no longer leaks them.

New API

  • RenderOptions on every render function:
    • scale: pixels per DIP/point, defaults to the screen scale.
    • opaque.
    • range: iOS only, 'standard' (default), 'extended' or 'automatic'.
    • bitmapConfig: Android only, 'ARGB_8888' (default) or 'RGB_565'.
    • Defaults keep the previous output. The old iOS API always drew into a standard-range buffer.
  • renderViewToImage(view, { width, height, host, ...renderOptions }): load, measure, render and dispose in one call.
  • withRenderedImage(view, callback, options): releases the native image (Utils.releaseNativeObject) when the callback returns, or when its promise settles. Rendered pixel memory is invisible to the JS GC, so discarded images otherwise pile up until an unrelated collection runs. The README documents calling Utils.releaseNativeObject directly for manual release.
  • The Angular ViewShotService forwards RenderOptions in captureInBackground and captureRenderedView.

Typing change

renderToImageSource, captureInBackground and captureRenderedView now return ImageSource | null.

Strict mode

The package (core and Angular entry point) now compiles with strict: true, so the shipped .d.ts files reflect real nullability. The only lib-check finding in strict consumers with skipLibCheck: false is TS2611 on LogicalViewContainer's ios/android getters, which core's own ProxyViewContainer raises identically.

Testing

  • nx run nativescript-view-shot:build.all and lint pass (lint now warning-free).
  • Typings checked from a strict consumer project: RenderOptions, renderViewToImage, withRenderedImage and the ImageSource | null return types all resolve as expected.
  • Not yet verified on device. Suggested check: render a 1200×100 view 100 times in a loop and compare the memory footprint with and without withRenderedImage / scale: 1.

- iOS: render with UIGraphicsImageRenderer instead of the deprecated
  UIGraphicsBeginImageContextWithOptions, which aborts the app on empty
  sizes or failed bitmap allocations
- add RenderOptions (scale, opaque, range, bitmapConfig) to all render
  functions; defaults keep the previous output
- renderToImageSource returns null for views without a native view or
  with an empty size instead of crashing
- android: attach background views to the foreground activity and don't
  crash disposing an already detached view
- add renderViewToImage, disposeImage and withRenderedImage
- angular: forward RenderOptions and always dispose background and
  Angular views, even when rendering or the delay callback throws
@nx-cloud

nx-cloud Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit bedc9b5

Command Status Duration Result
nx affected:lint ✅ Succeeded 1s View ↗

💡 Verify your cache is correct by running tasks in a sandbox. Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-10-10 02:19:23 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant