nnhubbard / ZSSRichTextEditor

A beautiful rich text WYSIWYG editor for iOS with a syntax highlighted source view
MIT License
3.78k stars 584 forks source link

在加入转base64的本地图片后,获取html,cpu占用率高达100%,严重卡死,你们都没遇到这个问题吗? #225

Open wu1132956426 opened 5 years ago

wu1132956426 commented 5 years ago

2f7d49d1-7c61-4d5e-8bdd-0d9077d9d8c0

gaojiewan commented 5 years ago

我也遇到这个问题了,不光是本地图片,只要html过长,加载都会这样,楼主的解决了吗?

mrZombie2016 commented 5 years ago

我把图片保存到本地了,不转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];
}
dev-wwj commented 5 years ago

当输入文本较长时 执行 $(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; });

gaojiewan commented 5 years ago

我试了楼上这个方法,对我这个不起作用啊,可能我描述问题不太清楚:我这是一次性加载大量html字符串,类似于文章的二次编辑,大神还有别的解决办法吗?

dev-wwj commented 5 years ago

我不清楚问题是否一致,如果是输入的过程中卡顿,cpu占用100%,可以用上面方法解决。我fork了,然后修改了一下,可以试试能不能解决你的问题 https://github.com/Scorpio-git/ZSSRichTextEditor

gaojiewan commented 5 years ago

问题不太一致,我这不是输入过程中卡顿,是一次性加载大量html会卡着,不过你这个也解决了我许多别的问题,感谢大神

Whyjsee commented 5 years ago

我把图片保存到本地了,不转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];
}

兄弟你这思路可以的👍

DamonRao commented 4 years ago

我把图片保存到本地了,不转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呢咋办

InsectQY commented 1 year ago

WKWebView 可以本地用 GCDWebServer 开一个 web 服务,img src 属性的 file:// 协议改成 http://localhost:端口/图片名称.png,记得在 plist 中开启 http 请求权限