【问题标题】:Pasting Image into UITextView Causes Crash将图像粘贴到 UITextView 会导致崩溃
【发布时间】:2012-11-09 02:02:03
【问题描述】:

谁能告诉我如何避免将图像粘贴到 UITextView 中导致的崩溃?

我的应用中有 UITextView,它允许对属性文本进行一些基本的编辑(iOS 6)。用户发现他们可以从网页复制文本和图像的组合,然后切换到我的应用程序并使用 UITextView 菜单 > 粘贴命令,这会使应用程序崩溃。我可以重现它,但我很难过如何处理它。崩溃的日志如下所示:

*** 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!'

当用户在另一个应用程序中使用复制命令时,我无法阻止他们选择图像。我也不想禁止在我的应用中粘贴。

【问题讨论】:

    标签: ios crash uitextview copy-paste paste


    【解决方案1】:

    这是一个可怕的 hack,但我确实找到了避免崩溃的方法。简而言之,我将 UITextView 子类化为覆盖粘贴命令,并将粘贴板转换为插入 UITextView 的字符串。

    - (void)paste:(id)sender {    
        UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
        NSString *string = pasteBoard.string;
        NSLog(@"Pasteboard string: %@", string);
    //  [pasteBoard setValue:@"Please work you miserable bastard!" forPasteboardType:(NSString *)kUTTypeUTF8PlainText];
    //  [super paste:sender]; // Doesn't work for unknown reason.
        [self insertText:string];
    }
    

    欢迎任何更好的建议...

    【讨论】:

    • 看起来不像一个可怕的黑客。如果您的应用程序支持,我认为将粘贴板转换为纯文本是非常合适的。
    • 我投了赞成票,主要是因为注释掉的部分!
    • 不起作用,请查看我对类似问题的回答:stackoverflow.com/a/35464829/1630623
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2012-12-03
    • 2012-06-28
    • 2015-03-27
    • 2011-07-05
    • 2020-04-19
    • 1970-01-01
    相关资源
    最近更新 更多