NSAttributedStringにある画像のサイズを変更すると奇妙な問題が発生します。サイズ変更の拡張機能は正常に動作していますが、イメージがNSAttributedStringに追加されると、何らかの理由で垂直方向に反転します。NSAttributedStringに追加すると画像が反転する
これはリサイズ拡張したものです:私はブレークポイントを設定し、画像を点検し、彼らはきた
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)
}
}
:
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
}
}
そしてここでは、画像の列挙は、属性付き文字列でありますすべてが正しい回転になりますが、次の行に達した場合を除きます。
newAttributedString.addAttribute(NSAttributedStringKey.attachment, value: newAttribute, range: range)
wh画像が垂直方向に反転される。
私はこの垂直フリップを引き起こしている可能性があります。これを修正する方法はありますか?