NSImage size not real size with some pictures?

后端 未结 4 481
太阳男子
太阳男子 2020-12-02 20:53

I see that sometimes NSImage size is not real size (with some pictures) and CIImage size is always real. I was testing with this image.

This is source code which I w

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-02 21:18

    Thanks to Zenopolis for the original ObjC code, here's a nice concise Swift version:

    func sizeForImageAtURL(url: NSURL) -> CGSize? {
            guard let imageReps = NSBitmapImageRep.imageRepsWithContentsOfURL(url) else { return nil }
            return imageReps.reduce(CGSize.zero, combine: { (size: CGSize, rep: NSImageRep) -> CGSize in
                return CGSize(width: max(size.width, CGFloat(rep.pixelsWide)), height: max(size.height, CGFloat(rep.pixelsHigh)))
            })
        }
    

提交回复
热议问题