RZRichTextView icon indicating copy to clipboard operation
RZRichTextView copied to clipboard

关于图片存到草稿,然后重新读取图片尺寸控制的问题。

Open kakashilw opened this issue 4 years ago • 3 comments

你好,首先感谢提供了这个库,以及很好的存储思路。

我运行你的demo,然后把用户选择的图片存到沙盒,然后再用这个方法

NSString *url = @"file://xxx/Documents/imageName.png";
NSString *html = [attr rz_codingToHtmlWithImagesURLSIfHad:@[url]];

获取到html,然后存储到草稿,然后再从草稿读取,也做了替换,图片可以正常读取出来,不过有个问题是

### 重新读取的图片不自适应UITextView的宽度了,而是直接显示的是原图的大小

不知道有什么方法可以让图片自适应吗?

kakashilw avatar Aug 14 '20 11:08 kakashilw

在选择图片之后插入到textView时,给图片在textview中的显示的尺寸做了一个限制(但是图片本身的尺寸没有改变),

- (void)insertImage:(UIImage *)image {
...
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    if (width > self.frame.size.width  - 20) {
        width = self.frame.size.width  - 20;
        height = height * width / image.size.width;
    }
    NSTextAttachment *attchment = [[NSTextAttachment alloc] init];
    attchment.bounds = CGRectMake(0, 0, width, height);
    NSMutableAttributedString *imageString = [NSMutableAttributedString attributedStringWithAttachment:attchment].mutableCopy;
   ...

在缓存(或上传到服务器)html之后,html中就是图片的本身的尺寸 所以在重新读取,显示到textView之后,又需要将图片重新设置一个显示的尺寸

具体可以下载demo,看看demo提供的方法, 供以借鉴

rztime avatar Aug 17 '20 07:08 rztime

你好,我下载了你的demo,但是没有找到关于重新显示到textVie之后,在重新对图片尺寸做出限制的逻辑,麻烦告知一下在哪里,谢谢

wuWeiFeng2 avatar Jun 23 '22 02:06 wuWeiFeng2

你好,我下载了你的demo,但是没有找到关于重新显示到textVie之后,在重新对图片尺寸做出限制的逻辑,麻烦告知一下在哪里,谢谢

应该是代码被我覆盖掉了,后边在补上, 两个思路, 一个是给html里img图片style 加一个max-width: 400px 具体值,这个就是显示的宽度 另一个是转换成NSAttributedString 之后,先遍历NSAttachment,修改里边的bounds,然后再设置到textView

rztime avatar Jun 28 '22 01:06 rztime

非常感谢大佬的指导、谢谢

---- 回复的原邮件 ---- | 发件人 | @.> | | 日期 | 2023年11月03日 16:05 | | 收件人 | @.> | | 抄送至 | @.>@.> | | 主题 | Re: [rztime/RZRichTextView] 关于图片存到草稿,然后重新读取图片尺寸控制的问题。 (#18) |

Closed #18 as completed.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

wuWeiFeng2 avatar Mar 05 '24 04:03 wuWeiFeng2