PyObjC: IOSurface Framework

12.1 · active · verified Tue Apr 14

PyObjC is a bridge between Python and Objective-C, enabling Python scripts to interact with and extend macOS's Cocoa libraries and other Objective-C frameworks. The `pyobjc-framework-iosurface` package provides Python wrappers specifically for Apple's IOSurface framework, which allows applications to share graphics buffers efficiently across processes. It is actively maintained, with version 12.1 being the latest, and sees regular updates to support new macOS SDKs and Python versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic IOSurface object with specified dimensions and pixel format. It highlights how Python dictionaries and primitive types are automatically bridged to their CoreFoundation equivalents for IOSurface creation. The resulting IOSurface object provides access to its properties, such as ID, width, and height.

import IOSurface

# Define properties for the IOSurface
# PyObjC automatically bridges Python dictionaries to CFDictionary
# and Python integers/booleans to CFNumber/CFBoolean.
properties = {
    IOSurface.kIOSurfaceWidth: 640,
    IOSurface.kIOSurfaceHeight: 480,
    IOSurface.kIOSurfaceBytesPerElement: 4, # e.g., 4 bytes per pixel for 32-bit color
    IOSurface.kIOSurfacePixelFormat: 0x42524741, # Example: 'ARGB' (Big-endian equivalent)
    # IOSurface.kIOSurfaceBytesPerRow: 640 * 4, # Can be derived or explicitly set
}

# Create the IOSurface
surface = IOSurface.IOSurfaceCreate(properties)

if surface:
    print(f"Successfully created IOSurface: {surface}")
    print(f"IOSurface ID: {IOSurface.IOSurfaceGetID(surface)}")
    print(f"IOSurface Width: {IOSurface.IOSurfaceGetWidth(surface)}")
    print(f"IOSurface Height: {IOSurface.IOSurfaceGetHeight(surface)}")
    # The 'surface' object's memory is managed by Objective-C's ARC,
    # but the Python wrapper will be garbage collected.
else:
    print("Failed to create IOSurface.")

view raw JSON →