【问题标题】:How to insert image in UIWebview at caret position?如何在插入符号位置的 UIWebview 中插入图像?
【发布时间】:2013-05-21 21:43:39
【问题描述】:

这是我目前用于插入图像的代码,但是,我的 webview 在插入后失去了焦点。但最奇怪的是,我还为 keyboardWillShowNotification 设置了通知;并在插入后调用此通知,但不存在键盘。

谁能给出这个问题的任何解决方案?

//allow me to programmatically bring up keyboard
_changWeiBo.keyboardDisplayRequiresUserAction = NO;
[_changWeiBo stringByEvaluatingJavaScriptFromString:@"document.getElementById('content').focus()"];

[_changWeiBo stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.execCommand('insertImage', false, '%@')", imagePath]];

//disallow me to programmatically bring up keyboard
_changWeiBo.keyboardDisplayRequiresUserAction = YES;

【问题讨论】:

  • 如何将图像插入 Web 视图?你用的是“addSubview”还是别的什么?
  • 这会破坏格式。我正在设计一个富文本编辑器,它需要图像与文本混合。

标签: javascript html iphone objective-c xcode


【解决方案1】:

我没有使用'insertImage' 命令,因为我需要图像的特定格式(在我的情况下,html 将保持拇指,点击时将全屏打开)。它也应该与insertImage 命令一起正常工作。这是objective-c代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSAssert([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeImage], @"Unhandled type: %@", [info objectForKey:UIImagePickerControllerMediaType]);
    // this is just to gather some paths for the picked image
    [_rtDelegate richEditor:self saveImage:[info objectForKey:UIImagePickerControllerEditedImage] completion:^(NSString *thumbPath, NSString *largePath, CGSize thumbSize) {
        [self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"docState.insertImage('%@', '%@', %d)", thumbPath, largePath, (int)thumbSize.height/2]];
    }];

    [picker dismissModalViewControllerAnimated:YES];
}
-(void)onTapInsertPhoto:(UIBarButtonItem *)sender{
    [self stringByEvaluatingJavaScriptFromString:@"docState.prepareInsertImage();"];
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate = self;
    imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
    imagePicker.allowsEditing = YES;

    UIViewController *vc = [[[UIApplication sharedApplication] keyWindow] rootViewController];
    [vc presentViewController:imagePicker animated:YES completion:nil];
    [imagePicker release];
}

这是 javascript(最重要的是 focusRange 和 backupRange):

this.prepareInsertImage = function(){
    try{
        backuprange();
    }
    catch(exc){
        log('prepareInertImage: ' + exc);
    }
}
this.insertImage = function(thumbPath, largePath, displayHeight){
    try{
        restorerange();
        // may work with the insertImage command here
        var imgwrap = document.createElement('a');
        imgwrap.href = largePath;
        imgwrap.setAttribute('href', 'rte:image:'+largePath);
        imgwrap.setAttribute('contenteditable', 'false');
        imgwrap.className = 'imgwrap';
        var img = document.createElement('img');
        img.setAttribute('src', thumbPath);
        img.setAttribute('height', displayHeight);
        imgwrap.appendChild(img);
        window.getSelection().getRangeAt(0).insertNode(imgwrap);
    }
    catch(exc){
        log('insertImage: ' + exc);
    }
}
var focusRange;
function backuprange(){
    var selection = window.getSelection();
    focusRange = selection.getRangeAt(0);
    focusRange.setEnd(focusRange.startContainer, focusRange.startOffset);
}
function restorerange(){
    var selection = window.getSelection();
    selection.removeAllRanges();
    selection.addRange(focusRange);
}

Objective-c 代码中不需要“docState”,javascript 代码中不需要“this”——我使用它们是因为我有一个在 javascript 中有更多方法的类,它也包含一些状态。

还要注意keyboardDisplayRequiresUserAction只适用于iOS 6.0+,所以以前的iOS系统不会自动显示键盘(但仍然会在正确的位置插入图像)。

【讨论】:

  • 你的 _rtDelegate 做什么?你如何让你的函数完成返回?
  • 嘿先生,如果可以的话,我想为这个答案付钱给你!它完美地工作。太糟糕了,我不能检查你的答案 100 次。
  • 我很高兴它成功了。代码中的_rtDelegate 处理保存拾取的图像,并返回存储它的路径(在本例中,也是拇指路径)。
  • 你介意教我如何编写带有完成块的代码吗?
  • window.getSelection().getRangeAt(0).insertNode(imgwrap);如果我想将图像插入特定的 div 怎么办?我该怎么做?
【解决方案2】:

感谢@alex-i,下面的这个解决方案(由我自己增强)完美地解决了当用户无法在插入符号位置插入图像时的错误,因为 UIWebview 在其他视图时失去焦点。

这是在插入符号位置插入图像的最小工作版本。

    this.prepareInsertImage = function(){
        try{
            backuprange();
        }
        catch(exc){
            log('prepareInsertImage: ' + exc);
        }
    }

    this.insertImage = function(imgPath){
        try{
            restorerange();
            document.execCommand("InsertImage",true,imgPath);
        }
        catch(exc){
            log('insertImage: ' + exc);
        }
    }
    var focusRange;
    function backuprange(){
        var selection = window.getSelection();
        focusRange = selection.getRangeAt(0);
        focusRange.setEnd(focusRange.startContainer, focusRange.startOffset);
    }
    function restorerange(){
        var selection = window.getSelection();
        selection.removeAllRanges();
        selection.addRange(focusRange);
    }

对于以后想要使用此代码的任何人,只需调用此代码:

    //Before UIImagePicker pops up
    [yourWebView stringByEvaluatingJavaScriptFromString:@"prepareInsertImage()"];

    //After UIImagePicker selects a image and you edit and store the image elsewhere
    NSString* imagePath = @"the path for your image....";
    [yourWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"insertImage('%@')",imagePath]];

【讨论】:

  • 这个解决方案完美地解决了我的问题。在此之前我尝试了一些方法(1.动态添加img到html字符串并重新加载uiwebview以添加图像,问题是在样式文本之间插入图像会很困难2.编程将uiwebview带到第一响应者拍照后,但没有成功)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-15
  • 1970-01-01
  • 2013-05-21
  • 2023-03-15
  • 2013-07-18
  • 2020-08-17
相关资源
最近更新 更多