How to get dpi / ppi UIImage?

How to get dpi / ppi images in iOS? Perhaps the raw image file contains this information, so can I get ppi / dpi from NSData? Thanks.

+4
source share
3 answers

To extract DPI from an image stored in NSData, include the Apple ImageIO infrastructure 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");

Please note that not all images contain DPI information. It may or may not be included in image metadata.

Flar49, , NSData. . http://iosdevelopertips.com/data-file-management/get-image-data-including-depth-color-model-dpi-and-more.html.

+4

Objective-C

:

CGFloat imageResolution = myImage.scale * 72.0f;

300 UIImage (72 dpi):

UIImage *my300dpiImage = [UIImage imageWithCGImage:mySourceImage.CGImage scale:300.0f/72.0f orientation:UIImageOrientationUp] ;

Swift

:

let imageResolution = myImage.scale * 72.0

:

// 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)
}

, , , 72 300 / , .

, , . , . , , , .

, -, , . , :

ios

+2

Swift 5: DPI . imageData NSData.

  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)   
0

All Articles