How to get dpi/ppi of image in iOS? Maybe raw image file contains this information, so I can get ppi/dpi from NSData? Thank you.
-
1A digital image has no DPI or PPI. Read this http://www.dpiphoto.eu/dpi.htm – borrrden Nov 14 '13 at 08:40
-
NSData is just binary data, as far as UIImage is concerned you may want to have a look at this http://stackoverflow.com/questions/15336789/how-to-get-uiimage-size-in-inches-or-uiimage-resolution – jbat100 Nov 14 '13 at 08:44
-
@borrrden When a digital image relates to a physical object there is certainly the potential for storing DPI, and while it is not needed for most applications, it is needed by some. e.g. a scanned document. – Richard Brightwell Oct 26 '14 at 11:41
3 Answers
To extract DPI from an image stored in NSData, include Apple's ImageIO framework in your project and use the following:
#import <ImageIO/ImageIO.h>
// Set your NSData up
NSData *data = some image data
// Get the DPI of the image
CGImageSourceRef imageRef = CGImageSourceCreateWithData((__bridge CFDataRef)(data), NULL);
CFDictionaryRef imagePropertiesDict = CGImageSourceCopyPropertiesAtIndex(imageRef, 0, NULL);
NSString *dpiHeight = CFDictionaryGetValue(imagePropertiesDict, @"DPIHeight");
NSString *dpiWidth = CFDictionaryGetValue(imagePropertiesDict, @"DPIWidth");
Note that not all images contain DPI information. It may or may not be included in the image's metadata.
This answer is based upon Flar49's answer, which is correct, but enhanced to get the information directly from NSData. See http://iosdevelopertips.com/data-file-management/get-image-data-including-depth-color-model-dpi-and-more.html for more information.

- 3,012
- 2
- 20
- 22
-
1this answer is produce nothing. it is always null,checked on 50 different images NSData. – Curnelious Sep 30 '14 at 09:40
-
1@Curnelious That is because of the software creating your images, as I point out in my answer. I assure you, some images do have DPI and I use this code every day to extract that DPI. Many times DPI is not important to software or people, so some programs don't bother to add it. Some people even mistakenly think DPI no longer matters. For me, it primarily comes into play when relating an image of a document to some new text I'm rastering onto it, but that's not a common scenario for most people. – Richard Brightwell Oct 26 '14 at 11:36
-
You should use the manifest constants `kCGImagePropertyDPIHeight` and `kCGImagePropertyDPIWidth` rather than string literals. Also, the documentation says the property value objects will be `CFNumber`s (which you can type-cast to `NSNumber` thanks to toll-free bridging), not strings. – Ken Thomases Dec 21 '14 at 09:33
Objective-C
Get image resolution:
CGFloat imageResolution = myImage.scale * 72.0f;
Create an image at 300dpi from a 'standard' UIImage ( 72 dpi ) :
UIImage *my300dpiImage = [UIImage imageWithCGImage:mySourceImage.CGImage scale:300.0f/72.0f orientation:UIImageOrientationUp] ;
Swift
Get resolution :
let imageResolution = myImage.scale * 72.0
Change image resolution :
// Just be sure source image is valid
if let source = mySourceImage, let cgSource = source.cgImage {
let my300dpiImage = UIImage(cgImage: cgSource, scale: 300.0 / 72.0, orientation: source.imageOrientation)
}
Note that I've added respect of the source image orientation in the swift example, and also note that changing a resolution from 72 to 300 dpi is leading to quality loss, since you don't have the picture details.
The big point is that resolution does not mean anything unless the image is rendered. Before that, you got the bits you got.. It's up to you to determine the final resolution, as seen by the viewer, by setting the destination rectangle size.
The resolution of the file, as fixed by the creator application, is not in the image data itself, but in the meta data. As explained in the previous answer, or at the address below:

- 2,607
- 24
- 23
Swift 5 solution: You can get the DPI information from the image metadata. imageData is of NSData type.
guard let imageSource = CGImageSourceCreateWithData(imageData, nil),
let metaData = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? [String: Any],
let dpi = metaData["DPIWidth"] as? Int else {
return
}
print(dpi)

- 141
- 1
- 7
-
1
-
If DPIWidth not works, that means there is no DPI info in the imageData. – Muhammed Tanriverdi Apr 15 '21 at 12:40