Find Image Dimensions from URL in IOS (SWIFT)

In this blog, you will learn how to find the image Dimensions from URL in IOS by using Swift Language. Please read the complete blog for better understandings.

Step-1

First, create a simple playground file from Xcode like the below screenshots.

Step-2

Import SwiftUI at top of the file like this

Step-3

Now create a let variable and give it an image URL.

For imageUrl, just copy the image address from the browser like the image below and paste it here like a string.

Step-4

After that write a function which is called “imageDimenssions” with a single string parameter which is called “url”. And this function will return the String in the end.

First, we need an image source which we will get from the CGImageSourceCreateWithURL function. CGImageSourceCreateWithURL function is taking two parameters, one is url of type CFURL and the other is CFDictionary. You know that our url is String type. So for converting it into the CFURL type, wrap it in URL(string: ) function and then force unwrap it as CFURL. This will convert the url type String to url type CFURL. For now, we will not use the CFDictionary parameter, so give it to nil value.

Now use this imageSource into the CGImageSourceCopyPropertiesAtIndex function and it will return the Dictionary of image properties.

let pixelWidth = imageProperties[kCGImagePropertyPixelWidth] as! Int

let pixelHeight = imageProperties[kCGImagePropertyPixelHeight] as! Int

Now by using these two lines, you can get the image Width and Height.

Step-5

After that call this function and print its value.

Now you can see the output of dimensions of the imageUrl.

So you can see the width & height of the imageUrl.

Hope you understand. If you have any issues then comment below. :)

I am a professional software engineer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store