J'ai un UITextView
dans lequel j'ajoute des images, avec ImagePickerController.UITextView retarde le défilement avec des images ajoutées
S'il n'y a que du texte dedans, c'est lisse, mais après avoir ajouté 3 ou 4 images il devient laggy on scroll dans son contenu.
Je compresse l'image en l'ajoutant, à sa qualité la plus basse, mais je dois faire quelque chose de mal parce qu'elle traîne toujours après la troisième image.
Voici le code:
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) }
}
Ce qui peut être à l'origine de cette question et toute astuce comment puis-je outrepasser cela?
Merci pour aider
EDIT
Merci à Duncan C J'ai réussi à éliminer le retard entier et augmenter les performances de rendu des images beaucoup.
par que j'ai remplacé mon imagePickerController
avec le contenu suivant:
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)
Une chose, je ne suis pas mettre l'image récupérée à un imageView, je l'ajouter en pièce jointe à la NSAttributedString; comment puis-je l'aspectFit it? –
Merci pour la réponse, je suis sûr que je vais résoudre mon problème avec votre explication –
Oh, je n'ai pas vu cette partie. Vous avez le code 'attachment.image = ...' Après avoir mis votre image à l'échelle, assignez-la à attachment.image. –