长按后选择复制图像时,迅速的uitextview html图像会导致崩溃 [英] swift uitextview html image causes crash when copy image is selected after long press

查看:91
本文介绍了长按后选择复制图像时,迅速的uitextview html图像会导致崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个UITextView,它显示一个NSAttributedString,其中包含带有文本和图像的HTML数据.这些数据是通过API接收的,因此图像和文本都被组合为一个HTML字符串.这是解析HTML的函数.

I currently have a UITextView that is displaying an NSAttributedString that contains HTML data with text and images. This data is received via API so images and text are all combined into one HTML string. This is the function that parses the HTML.

let htmlData = NSString(string: myString).data(using: String.Encoding.unicode.rawValue);
let options = [NSAttributedString.DocumentReadingOptionKey.documentType:
    NSAttributedString.DocumentType.html];
do{
    let text = try NSMutableAttributedString(data: htmlData ?? Data(), options: options, documentAttributes: nil);
    text.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "Arial", size: CGFloat(fontSize)) as Any, range: NSMakeRange(0, text.length));
    return text;
}
catch let error{
    print(error);
    return NSMutableAttributedString(string: myString);
}

长按图像时,会出现一个菜单,其中包含两个选项(1.复制图像2.保存到相机胶卷").当我单击复制图像"时,应用程序崩溃并显示以下错误消息:

When long pressing on the image, a menu appears with two options (1. Copy image 2. Save to Camera Roll). When I click on Copy image, the app crashes with this error message:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIConcretePasteboard setImage:]: Argument is not an object of type UIImage [(null)]'

有人知道如何解决此问题,以便在长按图像并选择复制图像"时不会崩溃?

Does anyone know how to fix this so when long pressing on the image and selecting Copy image, it will not crash?

推荐答案

您需要将图像从html转换为NSTextAttachment,就像将文本转换为NSAttributedString一样.并将这些附件附加到NSAttributedString上.

You need to convert images from html to NSTextAttachment, like you do it for text to NSAttributedString. And when attach these attachments to NSAttributedString.

它看起来可能像这样:

let htmlData = NSString(string: myString).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType:
    NSAttributedString.DocumentType.html]
let image = UIImage(named: IMAGENAME_FROM_HTML) ?? UIImage()
let imageAttachment = NSTextAttachment(image: image)

do {
    let text = try NSMutableAttributedString(data: htmlData ?? Data(), options: options, documentAttributes: nil)
    text.addAttribute(.font, value: UIFont(name: "Arial", size: CGFloat(fontSize), range: NSMakeRange(0, text.length))
    let textWithAttachment = try NSAttributedString(attachment: imageAttachment)
    text.replaceCharacters(in: NSMakeRange(RANGE_FOR_IMAGE_IN_HTML), with: textWithAttachment)
    return text
}
catch let error {
    print(error)
    return NSMutableAttributedString(string: myString)
}

P.S.不要在行尾迅速使用分号.

P.S. don't use semicolon on the lines end in swift.

这篇关于长按后选择复制图像时,迅速的uitextview html图像会导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆