UIImageView vs MTKView - iOS

BN
iOS World
Published in
4 min readJan 17, 2023
Photo by Héctor J. Rivas on Unsplash

UIImageView and MTKView are both classes in iOS that are used for displaying images, but they’re used for different purposes. UIImageView is a class for displaying a single image or vitality in a view. It’s a part of the UIKit frame, and is generally used for displaying small images in a stoner interface, similar as icons and thumbnails.
MTKView, on the other hand, is a class for displaying Metal content in a view. It is a part of the MetalKit framework, and is typically used for more intensive graphics rendering, such as 3D graphics or video playback.

UIImageView

UIImageView is a lightweight class that is simple to use and can be easily added to any view in an iOS app. It provides a number of properties and methods for controlling the display of the image, such as the image property for setting the image to be displayed, content mode property for controlling how the image is scaled and aligned, and animation Images property for displaying an animation. UIImageView also supports a variety of image file formats, such as PNG, JPEG, and GIF. Developers can also use it to display an image from the web by providing a URL.

Display an image in a UIImageView

let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))

// Set the image for the image view
let image = UIImage(named: "imageName")
imageView.image = image

// Add the image view to the current view
self.view.addSubview(imageView)

MTKView

MTKView provides a way to easily integrate Metal, Apple’s low-level, high-performance graphics and compute framework, into iOS apps. It provides a number of properties and methods for controlling the display of the Metal content, such as the delegate property for controlling the rendering loop, preferredFramesPerSecond property for controlling the frame rate, and enableSetNeedsDisplay property for controlling when the view should be redrawn.

The Metal framework provides a powerful and flexible way to create high-performance graphics and compute applications on iOS, and MTKView makes it easy to integrate Metal content into iOS apps. It’s commonly used in 3D gaming, video playback, and other high-performance applications that require a lot of graphics rendering.

It’s important to note that using the MTKView requires a deeper understanding of the Metal framework, as it’s not as simple as UIImageView. You will have to handle things such as creating a pipeline state, handling device changes and so on.

Display an image in an MTKView

import MetalKit

class MyViewController: UIViewController {
var mtkView: MTKView!

override func viewDidLoad() {
super.viewDidLoad()

// Create the MTKView
mtkView = MTKView(frame: view.frame)

// Set the device to use for rendering
mtkView.device = MTLCreateSystemDefaultDevice()

// Set the delegate for the view
mtkView.delegate = self

// Add the MTKView to the current view
view.addSubview(mtkView)
}
}

extension MyViewController: MTKViewDelegate {
func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
// Handle changes in the drawable size
}

func draw(in view: MTKView) {
// Create a command buffer
guard let commandBuffer = view.currentRenderCommandEncoder else { return }

// Create a texture from the image
let textureLoader = MTKTextureLoader(device: view.device!)
let texture = try! textureLoader.newTexture(name: "imageName", scaleFactor: view.contentScaleFactor, bundle: nil)

// Set the texture on the command buffer
commandBuffer.setFragmentTexture(texture, index: 0)

// Draw the texture
commandBuffer.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 6)

// End the encoding of the command buffer
commandBuffer.endEncoding()

// Present the drawable
view.present()
}
}

Difference between UIImageView and MTKView

  • Purpose: UIImageView is used to display simple images while MTKView is used to display Metal based graphics and video.
  • Complexity: UIImageView is a lightweight class that is simple to use, it can be easily added to any view in an iOS app, and can be used to display both static and animated images. MTKView, on the other hand, is a more complex class that requires a deeper understanding of the Metal framework, it’s commonly used for more intensive graphics rendering such as 3D graphics or video playback.
  • Performance: UIImageView is suitable for displaying simple images, but it’s not designed for high-performance graphics rendering. MTKView, on the other hand, is designed for high-performance graphics rendering and provides a way to easily integrate Metal, Apple’s low-level, high-performance graphics and compute framework, into iOS apps.
  • Framework: UIImageView is a part of the UIKit framework, and MTKView is a part of the MetalKit framework.
  • Image format: UIImageView supports a variety of image file formats, such as PNG, JPEG, and GIF, while MTKView uses Metal textures to display images.

--

--