Open wu1132956426 opened 5 years ago
我也遇到这个问题了,不光是本地图片,只要html过长,加载都会这样,楼主的解决了吗?
我把图片保存到本地了,不转base64,这样就不会让页面内容太多了,我改了这个代理方法:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info{
UIImage *selectedImage = info[UIImagePickerControllerEditedImage]?:info[UIImagePickerControllerOriginalImage];
//Scale the image
CGSize targetSize = CGSizeMake(selectedImage.size.width * self.selectedImageScale, selectedImage.size.height * self.selectedImageScale);
UIGraphicsBeginImageContext(targetSize);
[selectedImage drawInRect:CGRectMake(0,0,targetSize.width,targetSize.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Compress the image, as it is going to be encoded rather than linked
NSData *scaledImageData = UIImageJPEGRepresentation(scaledImage, kJPEGCompression);
NSString *filename;
if (@available(iOS 11.0, *)) {
NSURL *url = [info objectForKey:UIImagePickerControllerImageURL];
filename = [url lastPathComponent];
} else {
// Fallback on earlier versions
filename = [[[NSProcessInfo processInfo] globallyUniqueString] stringByAppendingString:@".jpeg"];
}
NSString *filePath = kBaseLibraryDirectoryPath(filename);
//缓存图片到本地
NSFileManager * fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:filePath]) {
[scaledImageData writeToFile:filePath atomically:YES];
}
//把图片插入到网页
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
if (fileUrl) {
[self insertImage:fileUrl.absoluteString alt:@""];
}
//Encode the image data as a base64 string
// NSString *imageBase64String = [scaledImageData base64EncodedStringWithOptions:0];
//Decide if we have to insert or update
// if (!self.imageBase64String) {
// [self insertImageBase64String:imageBase64String alt:self.selectedImageAlt];
// } else {
// [self updateImageBase64String:imageBase64String alt:self.selectedImageAlt];
// }
// self.imageBase64String = imageBase64String;
//Dismiss the Image Picker
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
当输入文本较长时 执行 $(document).on('selectionchange',function(e){ zss_editor.calculateEditorHeightWithCaretPosition(); zss_editor.setScrollPosition(); zss_editor.enabledEditingItems(e); }); 会造成卡顿 将 zss_editor.calculateEditorHeightWithCaretPosition(); 改成由interval控制而不是 selectionchange 可以解决此问题 完整代码 var flagContentChange = false; var interval = setInterval(function() { //如果文章内容发生了改变,并且过去一段时间500豪秒,则触发change if (flagContentChange) { zss_editor.calculateEditorHeightWithCaretPosition(); zss_editor.setScrollPosition(); zss_editor.enabledEditingItems(e); } // 重新统计这500豪秒内编辑器内容是否发生了改变 flagContentChange = false; }, 500); $(document).on('selectionchange',function(e){ flagContentChange = true; });
我试了楼上这个方法,对我这个不起作用啊,可能我描述问题不太清楚:我这是一次性加载大量html字符串,类似于文章的二次编辑,大神还有别的解决办法吗?
我不清楚问题是否一致,如果是输入的过程中卡顿,cpu占用100%,可以用上面方法解决。我fork了,然后修改了一下,可以试试能不能解决你的问题 https://github.com/Scorpio-git/ZSSRichTextEditor
问题不太一致,我这不是输入过程中卡顿,是一次性加载大量html会卡着,不过你这个也解决了我许多别的问题,感谢大神
我把图片保存到本地了,不转base64,这样就不会让页面内容太多了,我改了这个代理方法:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info{ UIImage *selectedImage = info[UIImagePickerControllerEditedImage]?:info[UIImagePickerControllerOriginalImage]; //Scale the image CGSize targetSize = CGSizeMake(selectedImage.size.width * self.selectedImageScale, selectedImage.size.height * self.selectedImageScale); UIGraphicsBeginImageContext(targetSize); [selectedImage drawInRect:CGRectMake(0,0,targetSize.width,targetSize.height)]; UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); //Compress the image, as it is going to be encoded rather than linked NSData *scaledImageData = UIImageJPEGRepresentation(scaledImage, kJPEGCompression); NSString *filename; if (@available(iOS 11.0, *)) { NSURL *url = [info objectForKey:UIImagePickerControllerImageURL]; filename = [url lastPathComponent]; } else { // Fallback on earlier versions filename = [[[NSProcessInfo processInfo] globallyUniqueString] stringByAppendingString:@".jpeg"]; } NSString *filePath = kBaseLibraryDirectoryPath(filename); //缓存图片到本地 NSFileManager * fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:filePath]) { [scaledImageData writeToFile:filePath atomically:YES]; } //把图片插入到网页 NSURL *fileUrl = [NSURL fileURLWithPath:filePath]; if (fileUrl) { [self insertImage:fileUrl.absoluteString alt:@""]; } //Encode the image data as a base64 string // NSString *imageBase64String = [scaledImageData base64EncodedStringWithOptions:0]; //Decide if we have to insert or update // if (!self.imageBase64String) { // [self insertImageBase64String:imageBase64String alt:self.selectedImageAlt]; // } else { // [self updateImageBase64String:imageBase64String alt:self.selectedImageAlt]; // } // self.imageBase64String = imageBase64String; //Dismiss the Image Picker [self.navigationController dismissViewControllerAnimated:YES completion:nil]; }
兄弟你这思路可以的👍
我把图片保存到本地了,不转base64,这样就不会让页面内容太多了,我改了这个代理方法:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info{ UIImage *selectedImage = info[UIImagePickerControllerEditedImage]?:info[UIImagePickerControllerOriginalImage]; //Scale the image CGSize targetSize = CGSizeMake(selectedImage.size.width * self.selectedImageScale, selectedImage.size.height * self.selectedImageScale); UIGraphicsBeginImageContext(targetSize); [selectedImage drawInRect:CGRectMake(0,0,targetSize.width,targetSize.height)]; UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); //Compress the image, as it is going to be encoded rather than linked NSData *scaledImageData = UIImageJPEGRepresentation(scaledImage, kJPEGCompression); NSString *filename; if (@available(iOS 11.0, *)) { NSURL *url = [info objectForKey:UIImagePickerControllerImageURL]; filename = [url lastPathComponent]; } else { // Fallback on earlier versions filename = [[[NSProcessInfo processInfo] globallyUniqueString] stringByAppendingString:@".jpeg"]; } NSString *filePath = kBaseLibraryDirectoryPath(filename); //缓存图片到本地 NSFileManager * fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:filePath]) { [scaledImageData writeToFile:filePath atomically:YES]; } //把图片插入到网页 NSURL *fileUrl = [NSURL fileURLWithPath:filePath]; if (fileUrl) { [self insertImage:fileUrl.absoluteString alt:@""]; } //Encode the image data as a base64 string // NSString *imageBase64String = [scaledImageData base64EncodedStringWithOptions:0]; //Decide if we have to insert or update // if (!self.imageBase64String) { // [self insertImageBase64String:imageBase64String alt:self.selectedImageAlt]; // } else { // [self updateImageBase64String:imageBase64String alt:self.selectedImageAlt]; // } // self.imageBase64String = imageBase64String; //Dismiss the Image Picker [self.navigationController dismissViewControllerAnimated:YES completion:nil]; }
你这个只是适用于UIWebView啊,WKWebView呢咋办
WKWebView
可以本地用 GCDWebServer
开一个 web 服务,img src 属性的 file:// 协议改成 http://localhost:端口/图片名称.png
,记得在 plist 中开启 http 请求权限