Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

CGImageSource.ImageAtIndex

Returns the image associated with the specified index in an image source.

Component Version macOS Windows Linux Server iOS SDK
CGImageSource 3.4 ✅ Yes ❌ No ❌ No ✅ Yes, on macOS ✅ Yes
MBS( "CGImageSource.ImageAtIndex"; Ref; Index { ; ImageType; FileName } )   More

Parameters

Parameter Description Example Flags
Ref The image source reference number returned by CGImageSource.CreateWithData, CGImageSource.CreateWithPath or CGImageSource.CreateWithURL $ref
Index The index of the image to query. -1 for global properties. Else a number from 0 to CGImageSource.Count-1. 0
ImageType The type of image to return.
Either JPEG, PNG, GIF or BMP. Default is JPEG.
"JPEG" Optional
FileName The filename to use for the picture.
Default is "image" with the extension matching the image type.
"test.jpg" Optional

Result

Returns image or error message.

Description

Returns the image associated with the specified index in an image source.
This function can be used to query previews of various image formats including RAW images. (everything Preview app can also open)

Examples

Convert image from one container and output to other:

Set Variable [ $img ; Value: MBS( "CGImageSource.CreateWithData"; test::input ) ]
Set Field [ test::output ; MBS( "CGImageSource.ImageAtIndex"; $img; 0; "PNG") ]
Set Variable [ $r ; Value: MBS( "CGImageSource.Release"; $img ) ]

Read HEIC image:

Set Variable [ $path ; Value: "/Users/cs/Desktop/IMG_3552.HEIC" ]
Set Variable [ $ImageSource ; Value: MBS( "CGImageSource.CreateWithPath"; $path) ]
If [ MBS("IsError") = 0 ]
    # you can get metadata as JSON to check later...
    Set Variable [ $$MetaData ; Value: MBS( "CGImageSource.Properties"; $ImageSource ) ]
    Set Variable [ $$ImageMetaData ; Value: MBS( "CGImageSource.Properties"; $ImageSource; 0 ) ]
    # get image to show
    Set Variable [ $image ; Value: MBS( "CGImageSource.ImageAtIndex"; $ImageSource; 0; "PNG"; "image.png") ]
    If [ MBS("IsError") = 0 ]
        Set Field [ Untitled::image ; $image ]
    End If
    Set Variable [ $r ; Value: MBS( "CGImageSource.Release"; $ImageSource ) ]
End If

See also

Blog Entries

This function checks for a license.

Created 18th August 2014, last changed 29th September 2020


CGImageSource.HasProperty - CGImageSource.Keys