I have a problem that I have been trying to solve for days. When resizing an image that's in a NSAttributedString using NSTextAttachment the vertical images are saved horizontally. The resizing extension is working fine, but when the image is added to the NSAttributedString, it gets flipped horizontally for some reason. I am still new to Xcode. Any help will be appreciate it! Thanks a lot!
This is my NSAttributedString extension:
extension NSAttributedString {
func attributedStringWithResizedImages(with maxWidth: CGFloat) -> NSAttributedString {
let text = NSMutableAttributedString(attributedString: self)
text.enumerateAttribute(NSAttributedString.Key.attachment, in: NSMakeRange(0, text.length), options: .init(rawValue: 0), using: { (value, range, stop) in
if let attachment = value as? NSTextAttachment {
let image = attachment.image(forBounds: attachment.bounds, textContainer: NSTextContainer(), characterIndex: range.location)!
if image.size.width > maxWidth {
let newImage = image.resizeImage(scale: maxWidth/image.size.width)
let newAttribut = NSTextAttachment()
newAttribut.image = newImage
text.addAttribute(NSAttributedString.Key.attachment, value: newAttribut, range: range)
}
}
})
return text
}
}
This is the image resizing extension:
extension UIImage {
func resizeImage(scale: CGFloat) -> UIImage {
let newSize = CGSize(width: self.size.width*scale, height: self.size.height*scale)
let rect = CGRect(origin: CGPoint.zero, size: newSize)
UIGraphicsBeginImageContext(newSize)
self.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}
I am using it here:
override func viewDidLoad() {
super.viewDidLoad()
let attributedText = self.getAttributedTextFromUserDefault(key: myNotes)
textView.attributedText = attributedText.attributedStringWithResizedImages(with: textView.bounds.size.width - 20)
}
Those are the functions to save and get the images on first place:
func saveAttributedTextToUserDefault(attributedText: NSAttributedString, key: String) {
do {
let data = try attributedText.data(from: NSRange(location: 0, length: attributedText.length), documentAttributes: [.documentType: NSAttributedString.DocumentType.rtfd])
userDefault.set(data, forKeyPath: myNotes)
} catch {
print(error)
}
}
func getAttributedTextFromUserDefault(key: String) -> NSAttributedString {
if let dataValue = userDefault.data(forKey: myNotes) as? Data {
do {
let attributeText = try NSAttributedString(data: dataValue, documentAttributes: nil)
return attributeText
} catch {
print("error: ", error)
}
}
return NSAttributedString()
}
figure out the rotation before I save the image
func rotateImage(image: UIImage) -> UIImage? {
if image.imageOrientation == UIImage.Orientation.up {
return image /// already upright, no need for changes
}
UIGraphicsBeginImageContext(image.size)
image.draw(in: CGRect(origin: CGPoint.zero, size: image.size))
let copy = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return copy
}
use
let originalImage = pickImage
attachment.image! = rotateImage(image: originalImage!)!