UIImage
height and width
iOS development
Swift programming
image dimensions

How can I get the height and width of an uiimage?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In iOS, getting the height and width of a UIImage seems straightforward, but there are two different measurement systems you need to keep straight:

  • points (image.size) for UI layout,
  • pixels (CGImage dimensions or points × scale) for image processing/export.

Many layout bugs come from mixing them accidentally. If you understand which unit your code path expects, extracting dimensions is trivial and reliable.

Core Sections

1. Get image size in points (UI layout use case)

For Auto Layout, view sizing, and display logic, use UIImage.size.

swift
1import UIKit
2
3func printPointSize(_ image: UIImage) {
4    let widthInPoints = image.size.width
5    let heightInPoints = image.size.height
6    print("points: \(widthInPoints) x \(heightInPoints)")
7}

This is what UIKit rendering APIs typically care about.

Also check scale:

swift
print("scale: \(image.scale)") // 1.0, 2.0, 3.0

A 100x100 point image at scale 3 has 300x300 pixels.

2. Get pixel dimensions for processing

For compression, CV pipelines, and exact bitmap operations, use pixel dimensions.

swift
1func pixelSize(of image: UIImage) -> CGSize {
2    if let cg = image.cgImage {
3        return CGSize(width: cg.width, height: cg.height)
4    }
5    // fallback if CGImage is unavailable
6    return CGSize(
7        width: image.size.width * image.scale,
8        height: image.size.height * image.scale
9    )
10}

When using CIImage-backed images, cgImage may be nil until rendered. In that case, render first via CIContext if strict pixel dimensions are required.

3. Integrate with image views and layout constraints

If you want an image view to match image aspect ratio:

swift
1func applyAspectRatioConstraint(imageView: UIImageView, image: UIImage) {
2    imageView.image = image
3    let ratio = image.size.width / image.size.height
4    let c = imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor, multiplier: ratio)
5    c.priority = .required
6    c.isActive = true
7}

For table/collection cells, avoid recalculating expensive dimensions repeatedly. Cache computed sizes if source images are reused.

4. Orientation caveat

UIImage has orientation metadata. Raw pixel width/height from cgImage may not reflect visual orientation after transforms.

If orientation matters for export or CV:

  1. normalize orientation by redrawing image,
  2. then read pixel dimensions.
swift
1func normalizedImage(_ image: UIImage) -> UIImage {
2    if image.imageOrientation == .up { return image }
3    let renderer = UIGraphicsImageRenderer(size: image.size)
4    return renderer.image { _ in
5        image.draw(in: CGRect(origin: .zero, size: image.size))
6    }
7}

Common Pitfalls

  • Using point dimensions where pixel-accurate dimensions are required.
  • Assuming size.width and size.height are raw bitmap pixels.
  • Ignoring image.scale, which leads to incorrect conversions between points and pixels.
  • Relying on cgImage dimensions without accounting for orientation metadata.
  • Recomputing dimensions in hot UI paths instead of caching in reusable views.

Summary

To get UIImage dimensions correctly, first decide whether you need points (UI layout) or pixels (processing). Use image.size for points and cgImage or size * scale for pixels. Account for orientation when exact visual dimensions matter. That unit discipline eliminates most iOS image-sizing bugs.

When working with remote images, avoid assuming dimensions before decode. Many networking libraries provide metadata headers, but those values can be absent or misleading after transformations. The safest approach is to inspect the final decoded UIImage used by the view layer, then cache the resulting dimensions alongside image identifiers. This prevents repeated decode work when the same image appears across list cells and detail screens. Caching width/height also improves pre-layout estimation and reduces visual jumps during asynchronous image loading.

If your app performs resizing for uploads, keep orientation normalization and dimension extraction in one utility function. Developers often read size, then later normalize orientation, accidentally changing effective pixel dimensions and producing wrong aspect ratios in server-side processing. A consistent utility that returns (normalizedImage, pointSize, pixelSize) in one call removes this class of bug. Add tests with portrait, landscape, and rotated EXIF images to ensure the function always reports expected dimensions.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.