accessing UIImage properties without loading in memory the image

后端 未结 3 1818
余生分开走
余生分开走 2020-12-07 15:53

As you know the iphone guidelines discourage loading uiimages that are greater than 1024x1024.

The size of the images that i would have to load varies, and i would l

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 16:40

    In Swift 5, with ImageIO,

    extension URL{
        var sizeOfImage: CGSize?{
            guard let imageSource = CGImageSourceCreateWithURL(self as CFURL, nil)
                , let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? [AnyHashable: Any]
                , let pixelWidth = imageProperties[kCGImagePropertyPixelWidth as String] as! CFNumber?
                , let pixelHeight = imageProperties[kCGImagePropertyPixelHeight as String] as! CFNumber?
                else {
                    return nil
            }
            var width: CGFloat = 0, height: CGFloat = 0
            CFNumberGetValue(pixelWidth, .cgFloatType, &width)
            CFNumberGetValue(pixelHeight, .cgFloatType, &height)
           }
           return CGSize(width: width, height: height)
        }
    }
    

    imageProperties[kCGImagePropertyOrientation as String] may be nil.

    It is nil, I tested with png image file

    as Apple says

    kCGImagePropertyOrientation

    The numeric value for this key encodes the intended display orientation for the image according to the TIFF and Exif specifications.

提交回复
热议问题