Skip to content

Cloudinary

Native Cloudinary image delivery for NativeScript.

The plugin supports:

  • A declarative ImageCloudinary component with an options property
  • Native Cloudinary SDK URL generation on both platforms
  • Top-level shorthand transformations, chained transformations, or raw transformation strings
  • Programmatic URL generation via generateUrl
  • Compatible with Angular, React, Solid, Svelte, and Vue

Installation

bash
npm install @nstudio/nativescript-cloudinary

Setup

Initialize Cloudinary once before using the component or calling generateUrl.

typescript
import { init } from '@nstudio/nativescript-cloudinary';

init('your_cloud_name', 'your_api_key', 'your_api_secret');

init accepts an optional fourth secure argument which defaults to true.

init requires your Cloudinary API secret. If your app should not embed signing credentials on-device, generate signed URLs on your server and pass the resulting URL into your app instead.

Usage

Core

xml
<Page
  xmlns="http://schemas.nativescript.org/tns.xsd"
  xmlns:cl="@nstudio/nativescript-cloudinary"
  navigatingTo="navigatingTo">
  <GridLayout class="p-24">
    <cl:ImageCloudinary
      stretch="aspectFill"
      options="{{ heroImage }}"
    />
  </GridLayout>
</Page>
typescript
const heroImage: ImageCloudinaryOptions = {
  src: 'cld-sample',
  width: 900,
  height: 900,
  crop: 'fill',
  gravity: 'face',
  format: 'auto',
  quality: 'auto',
};

Framework Registration

typescript
import { registerElement } from '@nativescript/angular';
import { ImageCloudinary } from '@nstudio/nativescript-cloudinary';

registerElement('ImageCloudinary', () => ImageCloudinary);
typescript
import { registerElement } from 'react-nativescript';
import { ImageCloudinary } from '@nstudio/nativescript-cloudinary';

registerElement('imageCloudinary', () => ImageCloudinary);
typescript
import { registerElement } from 'nativescript-vue';
import { ImageCloudinary } from '@nstudio/nativescript-cloudinary';

registerElement('ImageCloudinary', () => ImageCloudinary);
typescript
import { registerNativeViewElement } from '@nativescript-community/svelte-native/dom';
import { ImageCloudinary } from '@nstudio/nativescript-cloudinary';

registerNativeViewElement('imageCloudinary', () => ImageCloudinary);
typescript
import { registerElement } from 'dominative';
import { ImageCloudinary } from '@nstudio/nativescript-cloudinary';

registerElement('imageCloudinary', ImageCloudinary);

Transformation Modes

ImageCloudinaryOptions supports three transformation styles.

Top-level shorthand:

typescript
const options: ImageCloudinaryOptions = {
  src: 'cld-sample',
  width: 900,
  height: 900,
  crop: 'fill',
  gravity: 'face',
};

Chained transformations:

typescript
const options: ImageCloudinaryOptions = {
  src: 'cld-sample',
  transformations: [
    { width: 900, height: 900, crop: 'fill', gravity: 'auto' },
    { effect: 'sepia' },
    { radius: 'max' },
    { format: 'auto', quality: 'auto' },
  ],
};

Raw transformation string:

typescript
const options: ImageCloudinaryOptions = {
  src: 'cld-sample',
  rawTransformation: 'c_thumb,g_face,h_300,w_300/r_max/f_auto/q_auto',
};

Priority order: rawTransformation > transformations > top-level shorthand properties.

Programmatic URL Generation

Generate a Cloudinary URL without rendering the component:

typescript
import { generateUrl } from '@nstudio/nativescript-cloudinary';

const url = generateUrl({
  src: 'cld-sample',
  width: 600,
  crop: 'scale',
  format: 'auto',
  quality: 'auto',
});

API Reference

Functions

FunctionSignatureDescription
init(cloudName: string, apiKey: string, apiSecret: string, secure?: boolean) => voidInitializes the native Cloudinary SDK. Call once before using the plugin.
generateUrl(options: ImageCloudinaryOptions) => string | nullBuilds a Cloudinary delivery URL from the provided options. Returns null when input is missing or initialization has not happened yet.

Properties

PropertyTypeDescription
optionsImageCloudinaryOptionsRegistered NativeScript property on ImageCloudinary that builds the Cloudinary URL and assigns it to the underlying Image.src.

Classes

ClassDescription
ImageCloudinaryA drop-in Image subclass that renders Cloudinary-hosted assets from options.

ImageCloudinary also exposes a static debug flag:

typescript
import { ImageCloudinary } from '@nstudio/nativescript-cloudinary';

ImageCloudinary.debug = true;

Events

The plugin does not add custom events. Use the standard NativeScript Image events and lifecycle behavior when needed.

Types

TypeDescription
ImageCloudinaryOptionsTop-level configuration for the component or generateUrl.
CloudinaryTransformationA single transformation step in a chained transformation pipeline.
CropModeSupported Cloudinary crop modes such as fill, fit, scale, and thumb.
GravitySupported gravity modes such as auto, face, faces, and positional values.
ImageFormatSupported delivery formats such as auto, jpg, png, webp, and avif.

CloudinaryTransformation

Each CloudinaryTransformation entry supports the following fields:

PropertyTypeDescription
widthnumber | stringWidth
heightnumber | stringHeight
cropCropModeCrop mode such as fill, fit, scale, thumb, or pad
gravityGravityGravity such as auto, face, faces, or center
aspectRatiostring | numberAspect ratio
xnumber | stringHorizontal offset
ynumber | stringVertical offset
zoomnumber | stringZoom level
formatImageFormatDelivery format such as auto, webp, or avif
fetchFormatstringFetch format override
qualitystring | numberDelivery quality such as auto or 80
dprstring | numberDevice pixel ratio
effectstringEffect such as sepia, grayscale, or blur:800
radiusnumber | stringCorner radius, including max for circular crops
borderstringBorder string such as 5px_solid_rgb:0066ff
backgroundstringBackground color or special values such as auto:border
colorstringForeground color
colorSpacestringColor space
anglenumber | stringRotation angle or flip values
flagsstring | string[]Cloudinary transformation flags
overlaystringOverlay public ID or text spec
underlaystringUnderlay public ID
opacitynumber | stringOpacity from 0 to 100
pagenumber | stringPage or frame number
densitynumber | stringDPI density
defaultImagestringFallback public ID
namedstringNamed transformation
variablesRecord<string, string | number>User-defined Cloudinary variables
rawTransformationstringRaw transformation segment appended as-is

ImageCloudinaryOptions

ImageCloudinaryOptions adds delivery metadata on top of transformation settings:

PropertyTypeDescription
srcstringRequired Cloudinary public ID
resourceType'image' | 'video' | 'raw' | 'auto'Resource type, defaults to image
type'upload' | 'fetch' | 'private' | 'authenticated'Delivery type, defaults to upload
versionstringVersion string such as v1685472103
extensionstringFile extension override
transformationsCloudinaryTransformation[]Ordered chain of transformation components
rawTransformationstringFull raw transformation string

Top-level shorthand on ImageCloudinaryOptions also supports commonly used transformation fields such as width, height, crop, gravity, effect, radius, format, quality, dpr, background, border, color, angle, opacity, overlay, underlay, flags, aspectRatio, defaultImage, fetchFormat, zoom, x, y, page, density, and named.

For colorSpace or variables, use the transformations array.

License

Apache License Version 2.0