---
title: "withUnsafeBytes(as:_:)"
framework: testing
role: symbol
role_heading: Instance Method
path: "testing/attachableasimage/withunsafebytes(as:_:)"
---

# withUnsafeBytes(as:_:)

Encode a representation of this image in a given image format.

## Declaration

```swift
borrowing func withUnsafeBytes<R>(as imageFormat: AttachableImageFormat, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R
```

## Parameters

- `imageFormat`: The image format to use when encoding this image.
- `body`: A function to call. A temporary buffer containing a data representation of this instance is passed to it.

## Return Value

Return Value Whatever is returned by body.

## Discussion

Discussion note: Whatever is thrown by body, or any error that prevented the creation of the buffer. The testing library uses this function when saving an image as an attachment. The implementation should use imageFormat to determine what encoder to use.
