我没有使用该'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 系统不会自动显示键盘(但仍会在正确的位置插入图像)。