ImagePickerController를 사용하여 이미지를 추가하는 UITextView
이 있습니다.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에 편집
감사를 돕는
덕분에 나는 전체 지연을 제거하고 이미지를 많이 렌더링 성능을 향상시키기 위해 관리했습니다. 내가 대체 한 그함으로써
내 다음과 같은 내용으로 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)
한 가지, 내가 이미지 뷰에 검색된 이미지를 설정하고 있지 않다, 나는 NSAttributedString은에 첨부 파일로 추가 해요; 어떻게 aspectAit 할 수 있습니까? –
답변 주셔서 감사합니다. 귀하의 설명에 문제가 해결 될 것이라고 확신합니다. –
오, 그 부분을 보지 못했습니다. 당신은'attachment.image = ...'코드를 가지고 있습니다. 이미지를 스케일 한 후에 attachment.image에 할당하십시오. –