Reputation: 3246
I've a UITextView
in which I add images to, with ImagePickerController.
If there is only text in it, it is smooth, but after adding 3 or 4 images it becomes laggy on scroll within its content.
I compress the image when adding it, to it's lowest quality, but I must be doing something wrong because it still lags after the third image.
Here's the 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) }
}
What may be causing this issue and any tip how can I overpass this?
Thanks for helping
EDIT
Thanks to Duncan C I've managed to remove the whole lag and increase the performance rendering images A LOT.
By that I've replaced my imagePickerController
with the following content:
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)
Upvotes: 3
Views: 502
Reputation: 83
Its not the best practice to do that. UITextViews is meant for text not all views.
you need to make a custom UIView consisting of a 1, textview 2, UICollectionView for numerous images.
In this way you can reuse this custom view at numerous places too throughout the project
Upvotes: 1
Reputation: 131481
You're misusing the scale factor. That's intended for handling the 1x, 2x, and 3x scaling of normal, retina, and @3x retina images.
The way you're doing it, the system has to render the full-sized images into your bounds rectangles each time it wants to draw one. If the images are considerably bigger than the size you're displaying them, you waste a bunch of memory and processing time.
First try simply setting your image view's contentMode
to .scaleAspectFit
and install the original image in image view without mucking around with the scale factor. See how that performs.
If that doesn't give acceptable performance you should render your starting images into an off-screen graphics context that's the target size of your image, and install the re-rendered image into your image views. You can use UIGraphicsBeginImageContextWithOptions()
or various other techniques to resize your images for display.
Take a look at this link for information on image scaling and resizing:
http://nshipster.com/image-resizing/
Upvotes: 2