有人可以指导我如何从粘贴板上的属性文本中删除图像吗?
实施例
我有一个支持NSAttributedString的UITextView。如果我允许用户从包含图像的粘贴板粘贴某些内容(例如图片和从Safari应用程序中的网页复制的一些文本),当我尝试保存它时,它会崩溃我的应用程序:
*** Assertion failure in -[NSHTMLReader _addAttachmentForElement:URL:needsParagraph:usePlaceholder:], /SourceCache/UIFoundation_Sim/UIFoundation-78/UIFoundation/TextSystem/NSHTMLReader.m:1478
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to find missing_image.tiff!'
仅当图像是源副本的一部分时才会发生这种情况。为什么它指的是tiff我不知道,因为我一直在测试的源图像是jpg和png。
如related post with slightly different goal中所述,我避免崩溃的解决方法是强制将粘贴板强制为常规字符串:
- (void)paste:(id)sender
{
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
NSString *string = pasteBoard.string;
[self insertText:string];
}
这可以防止崩溃,但它也可以防止用户粘贴任何格式化的文本。说出可能来自Mail应用程序的粗体或斜体。
我可以说,我需要从粘贴板上剥离任何图像,以便保留文本格式。谢谢你的帮助。