2017-04-15 121 views
2

我有一個UITextView我添加圖像,與ImagePickerController。UITextView滯後滾動添加圖像

如果其中只有文本,它是平滑的,但添加3或4張圖片後,其內容中的滾動會變得遲緩。

我在添加圖像時會壓縮圖像,因爲圖像的質量最差,但是我必須做一些錯誤的操作,因爲它仍然滯後於第三張圖像。

下面的代碼:

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 I已經設法刪除整個滯後和提高性能的渲染圖像了很多。

通過,我已經取代了我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) 

回答

1

你濫用比例因子。這是爲了處理正常,視網膜和@ 3x視網膜圖像的1倍,2倍和3倍縮放。

你這樣做的方式,系統必須將全尺寸圖像渲染到邊界矩形中,每次它要繪製一個矩形。如果圖像比您顯示的圖像大得多,則會浪費一大堆內存和處理時間。

首先嚐試簡單地將圖像視圖的contentMode設置爲.scaleAspectFit,然後將原始圖像安裝在圖像視圖中,而無需考慮比例因子。看看如何執行。

如果不給,你應該使你的啓動映像導入到你的圖像的目標尺寸屏幕外的圖形上下文可接受的性能,並重新繪製圖像安裝到您的圖像視圖。您可以使用UIGraphicsBeginImageContextWithOptions()或各種其他技術來調整圖像的大小以供顯示。

看看這個鏈接的信息,圖像縮放和調整:

http://nshipster.com/image-resizing/

+0

有一兩件事,我沒有檢索到的圖像設置爲一個ImageView的,我將其添加爲附件的NSAttributedString;我該如何aspectFit呢? –

+0

感謝您的回答我相信我會解決我的問題與您的解釋 –

+0

哦,我沒有看到這一部分。你有代碼'attachment.image = ...'縮放你的圖像後,將其分配給attachment.image。 –

1

它不是最好的做法,以做到這一點。 UITextViews適用於文本而非所有視圖。

您需要製作一個由1,textview 2,UICollectionView組成的自定義UIView,用於許多圖像。

這樣您可以重新過整個項目在許多地方這個自定義視圖

+0

嗯......這實際上是一個不錯的提示,謝謝 –

+0

如果你將這個答案掩蓋爲有用的話,我將非常感激:) – chetan