如何获得UIImage的dpi / ppi? [英] How to get dpi/ppi of UIImage?
问题描述
如何在iOS中获取图像的dpi / ppi?也许原始图像文件包含这些信息,所以我可以从NSData获得ppi / dpi?
谢谢。
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.
推荐答案
要从存储在NSData中的图像中提取DPI,请在项目中包含Apple的ImageIO框架,使用以下内容:
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");
请注意,并非所有图像都包含DPI信息。它可能包含也可能不包含在图像的元数据中。
Note that not all images contain DPI information. It may or may not be included in the image's metadata.
这个答案是基于Flar49的答案,这是正确的,但增强了直接从NSData获取信息。请参阅 http: //iosdevelopertips.com/data-file-management/get-image-data-including-depth-color-model-dpi-and-more.html 了解更多信息。
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.
这篇关于如何获得UIImage的dpi / ppi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!