这是我目前用于插入图片的代码,但是,我的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;
答案 0 :(得分:5)
感谢@ 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 :(得分:3)
我没有使用'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系统不会自动显示键盘(但仍会将图像插入正确的位置)。