How to get dpi/ppi of UIImage?

前端 未结 3 873
清酒与你
清酒与你 2021-01-06 01:56

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.

3条回答
  •  盖世英雄少女心
    2021-01-06 02:26

    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)   
    

提交回复
热议问题