ImagePickerControllerで画像を追加するUITextView
があります。UITextViewは追加された画像でスクロールします
テキストがある場合はスムーズですが、3つまたは4つの画像を追加すると、そのコンテンツ内のスクロールで遅くなります。
画像を追加するときに画像を圧縮するのは最低の画質ですが、3枚目の画像よりもまだ遅れているので、何か間違っている必要があります。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let _image = info[UIImagePickerControllerOriginalImage] as? UIImage else { return }
self.dismiss(animated: true, completion: nil)
// Compress the retrieved image
let compressedImage = UIImage(data: _image.lowestQualityJPEGNSData!)
//create and NSTextAttachment and add your image to it.
let attachment = NSTextAttachment()
let oldWidth = compressedImage?.size.width // scales it within the UITextView
let scaleFactor = oldWidth!/(bodyEditText.frame.size.width - 10); // adjusts the desired padding
attachment.image = UIImage(cgImage: (compressedImage?.cgImage!)!, scale: scaleFactor, orientation: .up)
//put your NSTextAttachment into and attributedString
let attString = NSAttributedString(attachment: attachment)
//add this attributed string to the current position.
bodyEditText.textStorage.insert(attString, at: bodyEditText.selectedRange.location)
}
extension UIImage {
var uncompressedPNGData: Data? { return UIImagePNGRepresentation(self) }
var highestQualityJPEGNSData: Data? { return UIImageJPEGRepresentation(self, 1.0) }
var highQualityJPEGNSData: Data? { return UIImageJPEGRepresentation(self, 0.75) }
var mediumQualityJPEGNSData: Data? { return UIImageJPEGRepresentation(self, 0.5) }
var lowQualityJPEGNSData: Data? { return UIImageJPEGRepresentation(self, 0.25) }
var lowestQualityJPEGNSData:Data? { return UIImageJPEGRepresentation(self, 0.0) }
}
この問題、私はこれを乗り越えることができますどのように任意のヒントを引き起こす可能性のあるもの:
ここでは、コードですか?ダンカンCにEDIT
感謝を支援するための
おかげで私は、全体の遅れを削除し、画像をたくさんレンダリングのパフォーマンスを向上させることができました。私は交換してきたということで
私の次の内容のimagePickerController
:
let size = __CGSizeApplyAffineTransform(_image.size, CGAffineTransform.init(scaleX: 0.3, y: 0.3))
let hasAlpha = false
let scale: CGFloat = 0.0 // Automatically use scale factor of main screen
UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale)
_image.draw(in: CGRect(origin: CGPoint.zero, size: size))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let att = NSTextAttachment()
att.image = scaledImage
//put your NSTextAttachment into and attributedString
let attString = NSAttributedString(attachment: att)
//add this attributed string to the current position.
bodyEditText.textStorage.insert(attString, at: bodyEditText.selectedRange.location)
一つのこと、私はImageViewのに検索された画像を設定していないよ、私はNSAttributedStringへの添付ファイルとして、それを追加しています。どのように私はそれをaspectFitすることができますか? –
答えをありがとう私はあなたの説明で私の問題を解決すると確信しています –
ああ、私はその部分を見ませんでした。あなたは 'attach.image = ... 'というコードを持っています。イメージを拡大/縮小した後、attachment.imageに割り当てます。 –