插入图片,上传服务器 看这里
直接上代码了。
1.把需要编辑的内容里面的照片过滤出来
- (NSArray *) getImageurlFromHtml:(NSString *) webString
{
NSMutableArray * imageurlArray = [NSMutableArray arrayWithCapacity:1];
//标签匹配
NSString *parten = @"<img(.*?)>";
NSError* error = NULL;
NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:parten options:0 error:&error];
NSArray* match = [reg matchesInString:webString options:0 range:NSMakeRange(0, [webString length] - 1)];
for (NSTextCheckingResult * result in match) {
//获取数组中的标签
NSRange range = [result range];
NSString * subString = [webString substringWithRange:range];
//从图片中的标签中提取ImageURL
NSRegularExpression *subReg = [NSRegularExpression regularExpressionWithPattern:@"http://[^'|\"|\\s|>]*" options:0 error:NULL];
NSArray* match = [subReg matchesInString:subString options:0 range:NSMakeRange(0, [subString length] - 1)];
NSTextCheckingResult * subRes = match[0];
NSRange subRange = [subRes range];
subRange.length = subRange.length ;
NSString * imagekUrl = [subString substringWithRange:subRange];
//将提取出的图片URL添加到图片数组中
[imageurlArray addObject:imagekUrl];
}
return imageurlArray;
}
2.因为把html 转成富文本(转完之后 图片attechment里面的userinfo是空的),所以需要给userinfo附上值,也就是上面获取的image
- (void)handlerAllAttechmentWith:(NSAttributedString *)StringString withimagesArr:(NSArray *)imageUrl{
NSMutableArray *attachmentArr=[NSMutableArray array];
NSRange effectiveRange = NSMakeRange(0, 0);
while (effectiveRange.location + effectiveRange.length < StringString.length) {
NSDictionary *attributes = [StringString attributesAtIndex:effectiveRange.location effectiveRange:&effectiveRange];
NSTextAttachment *attachment = attributes[@"NSAttachment"];
if (attachment) {
[attachmentArr addObject:attachment];
}
effectiveRange = NSMakeRange(effectiveRange.location + effectiveRange.length, 0);
}
if (attachmentArr.count == imageUrl.count) {
for (int i=0; i<attachmentArr.count; i++) {
NSTextAttachment *att=attachmentArr[i];
//userinfo这个属性是延展,看上一篇
att.userInfo= imageUrl[i];
}
}
}
3.接下来的操作,按照插入图片,上传服务器走就可以。
在这里,只是简单实现图片的更换,文字的修改。涉及到字体大小,颜色等操作,后期慢慢加入。