Loading Images
useImage Hook
TheuseImage hook is the primary way to load images. It returns an SkImage instance or null while loading.
Error Handling
The hook provides an optional error handler:MakeImageFromEncoded
Create images from encoded data:MakeImage
Create images from raw pixel data:Image Component
TheImage component displays images on the canvas.
Props
| Name | Type | Description |
|---|---|---|
| image | SkImage | The image instance to display |
| x | number | Left position of the destination |
| y | number | Top position of the destination |
| width | number | Width of the destination |
| height | number | Height of the destination |
| fit | Fit | How the image fits in the rectangle (default: contain) |
| sampling | Sampling | Sampling method for quality control |
Basic Example
Fit Modes
Control how images fit within their bounds:contain
Fits the entire image within the bounds, maintaining aspect ratio:cover
Covers the entire bounds, may crop the image:fill
Stretches the image to fill the bounds:fitWidth
Scales to match width, maintains aspect ratio:fitHeight
Scales to match height, maintains aspect ratio:scaleDown
Like contain, but never scales up:none
No fitting applied, uses image’s natural size:Sampling Options
Control image quality and performance:Cubic Sampling (Best Quality)
Use cubic sampling for high-quality rendering:Custom Cubic Sampling
Filter and Mipmap Modes
Image Instance Methods
Once loaded,SkImage instances provide useful methods:
Dimensions
Encoding
Export images to various formats:Reading Pixels
Creating Shaders
Advanced Techniques
Image Clipping
Image Transformations
Image with Effects
Performance Tips
- Cache loaded images with
useImage - Use appropriate sampling modes for your use case
- Consider image dimensions - smaller is faster
- Preload images when possible
- Use
fitWidthorfitHeightwhen appropriate - Optimize image formats (WebP, JPEG) before bundling
Common Patterns
Conditional Rendering
Multiple Images
See Also
- Animated Images - GIF and animated WebP
- SVG - Vector graphics
- Video - Video frames
- Image Shaders - Using images as shaders