从HTML获取图像大小 [英] Getting image size from HTML

查看:114
本文介绍了从HTML获取图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从博客解析RSS提要,并将帖子的HTML放在 UIWebView 中。



但现在我想改变图像的大小以适应iPhone屏幕。我试着用下面的代码替换

  HTMLContent = [HTMLContent stringByReplacingOccurrencesOfString:@width = \480\ withString:@ 宽度= \ 300\ ]; 

但是,这样做只会替换宽度为480的图片。而且我不会改变身高!



你知道是否有一种方法可以用300代替任何宽度,并以相同的因子改变高度?

解决方案

您可以使用正则表达式来执行此操作。您需要匹配字符串中的< img> 标签,并提取高度和宽度值,然后计算新的所需高度和宽度,然后替换高度和宽度与新值的字符串。

下面的代码应该可以工作,但它可能会丢失一些边缘情况(例如,如果height属性出现在width属性之前 img 标记)。
NSString * originalHTML = @< html>< body>< img src ='image1.gif'width ='4800'height = '80'alt ='foobar'/>< img src ='image1.gif'width ='70000'height ='99999'alt ='foobar'/>< / body>< / html>;

NSString * regexPattern = @< img [^>] * width = ['\\\] *([0-9] +)[^>] *高度= ['\ \\s] *([0-9] +)[^>] *> 中;

NSRegularExpression * regex =
[NSRegularExpression regularExpressionWithPattern:regexPattern
options:NSRegularExpressionDotMatchesLineSeparators
error:nil];

NSMutableString * modifiedHTML = [NSMutableString stringWithString:originalHTML];

NSArray * matchesArray = [regex matchesInString:modifiedHTML
options:NSRegularExpressionCaseInsensitive
range:NSMakeRange(0,[modifiedHTML length])];;

NSTextCheckingResult * match;

//需要计算偏移量,因为在修改字符串后,HTML字符串中匹配
//的范围位置将会改变
int offset = 0,newoffset = 0;
$ b $ for(match matches); {

NSRange widthRange = [match rangeAtIndex:1];
NSRange heightRange = [match rangeAtIndex:2];

widthRange.location + = offset;
heightRange.location + = offset;

NSString * widthStr = [modifiedHTML substringWithRange:widthRange];
NSString * heightStr = [modifiedHTML substringWithRange:heightRange];

int width = [widthStr intValue];
int height = [heightStr intValue];

if(width> maxWidth){
height =(height * maxWidth)/ width;
width = maxWidth;

NSString * newWidthStr = [NSString stringWithFormat:@%d,width];
NSString * newHeightStr = [NSString stringWithFormat:@%d,height];

[modifiedHTML replaceCharactersInRange:widthRange withString:newWidthStr];

newoffset =([newWidthStr length] - [widthStr length]);
heightRange.location + = newoffset;

[modifiedHTML replaceCharactersInRange:heightRange withString:newHeightStr];

newoffset + =([newHeightStr length] - [heightStr length]);
offset + = newoffset;
}
}

NSLog(@%@,modifiedHTML);


I'm parsing an RSS feed from a blog, and putting the HTML of a post inside a UIWebView.

But now I would like to change the size of the images to fit the iPhone screen. I'm trying with the following replace

HTMLContent = [HTMLContent stringByReplacingOccurrencesOfString:@"width=\"480\"" withString:@"width=\"300\""];

But doing like this I'm replacing only the images with 480 width. And moreover I'm not changing the height!

Do you know if there is a way to replace any width with 300, and changing the height by the same factor?

解决方案

You can do this using a Regular Expression. You'll want to match the <img> tags within the string and extract the height and width values, then calculate your new desired height and width, and then replace the height and width in the string with the new values.

The following code should work, but it may miss some edge cases (such as if the height attribute appears before the width attribute in the img tag).

int maxWidth = 300;
NSString *originalHTML = @"<html><body><img src='image1.gif' width='4800' height='80' alt='foobar'/><img src='image1.gif' width='70000' height='99999' alt='foobar'/></body></html>";

NSString *regexPattern = @"<img[^>]*width=['\"\\s]*([0-9]+)[^>]*height=['\"\\s]*([0-9]+)[^>]*>";

NSRegularExpression *regex = 
[NSRegularExpression regularExpressionWithPattern:regexPattern 
                                          options:NSRegularExpressionDotMatchesLineSeparators 
                                            error:nil];

NSMutableString *modifiedHTML = [NSMutableString stringWithString:originalHTML];

NSArray *matchesArray = [regex matchesInString:modifiedHTML 
                                   options:NSRegularExpressionCaseInsensitive 
                                     range:NSMakeRange(0, [modifiedHTML length]) ]; 

NSTextCheckingResult *match;

// need to calculate offset because range position of matches
// within the HTML string will change after we modify the string
int offset = 0, newoffset = 0;

for (match in matchesArray) {

    NSRange widthRange = [match rangeAtIndex:1];
    NSRange heightRange = [match rangeAtIndex:2];

    widthRange.location += offset;
    heightRange.location += offset;

    NSString *widthStr = [modifiedHTML substringWithRange:widthRange];
    NSString *heightStr = [modifiedHTML substringWithRange:heightRange];

    int width = [widthStr intValue];
    int height = [heightStr intValue];

    if (width > maxWidth) {
        height = (height * maxWidth) / width;
        width = maxWidth;

        NSString *newWidthStr = [NSString stringWithFormat:@"%d", width];
        NSString *newHeightStr = [NSString stringWithFormat:@"%d", height];

        [modifiedHTML replaceCharactersInRange:widthRange withString:newWidthStr];

        newoffset = ([newWidthStr length] - [widthStr length]);
        heightRange.location += newoffset;

        [modifiedHTML replaceCharactersInRange:heightRange withString:newHeightStr];                

        newoffset += ([newHeightStr length] - [heightStr length]);            
        offset += newoffset;
    }
}

NSLog(@"%@",modifiedHTML);

这篇关于从HTML获取图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆