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.
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 (
CGImagedimensions 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.
This is what UIKit rendering APIs typically care about.
Also check scale:
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.
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:
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:
- normalize orientation by redrawing image,
- then read pixel dimensions.
Common Pitfalls
- Using point dimensions where pixel-accurate dimensions are required.
- Assuming
size.widthandsize.heightare raw bitmap pixels. - Ignoring
image.scale, which leads to incorrect conversions between points and pixels. - Relying on
cgImagedimensions 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
- How can I get the iOS 7 default blue color programmatically?
- How can I import Swift code to Objective-C?
- How can I increase the Tap Area for UIButton?
- How can I launch Safari from an iPhone app?
- How can I launch the iOS Simulator from Terminal?
- How can I load storyboard programmatically from class?
- How can I log each request/response using Alamofire?
- How can I make a button have a rounded border in Swift?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.