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.
First, create a simple playground file from Xcode like the below screenshots.
Import SwiftUI at top of the file like this
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.
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.
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. :)