Images being flipped when adding to NSAttributedSt

2019-07-27 08:07发布

问题:

I have a strange problem when resizing an image that's in a NSAttributedString. The resizing extension is working fine, but when the image is added to the NSAttributedString, it gets flipped vertically for some reason.

This is the resizing extension:

extension NSImage {
  func resize(containerWidth: CGFloat) -> NSImage {

    var scale : CGFloat = 1.0
    let currentWidth = self.size.width
    let currentHeight = self.size.height

    if currentWidth > containerWidth {
      scale = (containerWidth * 0.9) / currentWidth
    }

    let newWidth = currentWidth * scale
    let newHeight = currentHeight * scale

    self.size = NSSize(width: newWidth, height: newHeight)

    return self
  }
}

And here is the enumeration over the images in the attributed string:

newAttributedString.enumerateAttribute(NSAttributedStringKey.attachment, in: NSMakeRange(0, newAttributedString.length), options: []) { value, range, stop in
    if let attachement = value as? NSTextAttachment {
        let image = attachement.image(forBounds: attachement.bounds, textContainer: NSTextContainer(), characterIndex: range.location)!

        let newImage = image.resize(containerWidth: markdown.bounds.width)
        let newAttribute = NSTextAttachment()
        newAttribute.image = newImage
        newAttributedString.addAttribute(NSAttributedStringKey.attachment, value: newAttribute, range: range)
    }
}

I've set breakpoints and inspected the images, and they are all in the correct rotation, except when it reaches this line:

newAttributedString.addAttribute(NSAttributedStringKey.attachment, value: newAttribute, range: range)

where the image gets flipped vertically.

I have no clue what could be causing this vertical flip. Is there a way to fix this?

回答1:

If you look at the developer docs for NSTextAttachment:

https://developer.apple.com/documentation/uikit/nstextattachment

The bounds parameter is defined as follows:

“Defines the layout bounds of the receiver's graphical representation in the text coordinate system.”

I know that when using CoreText to layout text, you need to flip the coordinates, so I should imagine you need to transform your bounds parameter with a vertical reflection too.

Hope that helps.



回答2:

I figured it out and it was so much simpler than I was making it.

Because the image was in a NSAttribuetdString being appended into a NSTextView I didn't need to resize each image in the NSAttributedString, rather I just had to set the attachment scaling inside the NSTextView with

markdown.layoutManager?.defaultAttachmentScaling = NSImageScaling.scaleProportionallyDown 

One line is all it took