Contents

CVAttachmentAccess

Provides access to the attachments of a buffer.

Declaration

@dynamicMemberLookup struct CVAttachmentAccess<Keys> where Keys : CVAttachmentKeyDefinitions

Overview

Lifetime of an instance of this type is tied to the lifetime of the buffer carrying attachments. The properties of this object are dynamically resolved to the static properties of the Keys type.

For example, when custom attachment keys are defined as follows:

extension CVImageBufferAttachmentKeyDefinitions {
	static var imageBufferName: Key<ShouldPropagate, String> {
		"com.app.imageBufferName"
	}
}
extension CVPixelBufferAttachmentKeyDefinitions {
	static var pixelBufferNumber: Key<ShouldPropagate, Int> {
		"com.app.pixelBufferNumber"
	}
}

Both keys can be accessed as a property of an CVAttachmentAccess instance. As CVImageBufferAttachmentKeyDefinitions is the superclass of CVPixelBufferAttachmentKeyDefinitions.

func inspect(attachments: borrowing CVAttachmentAccess<CVPixelBufferAttachmentKeyDefinitions>) {
	let value1: String? = attachments.imageBufferName
	let value2: Int? = attachments.pixelBufferNumber
}

It is also possible to access the keys by directly specifying raw string value:

let num: Int? = pixelBuffer.attachments["com.app.pixelBufferNumber"]
pixelBuffer.attachments["com.app.pixelBufferNumber"] = (100, .shouldPropagate)

// To set an attachment value by ignoring the preferred mode requires using rawValue of the key
pixelBuffer.attachments[CVPixelBufferAttachmentKeyDefinitions.displayDimensions.rawValue] = (CGSize(width: 600, height: 400), .shouldNotPropagate)

Topics

Instance Methods

Subscripts

See Also

Attachment system (Swift)