Hey all,
I'm trying to store a few pages locally so I can still load them up in my webview when I don't have a network connection.
Now the saving goes well, only problem I'm facing is with images.
I'm trying to get the content between <img src=\" and \"
So I can store this image locally as well and rewrite the code a little bit.
I can find all the locations of each <img src=\"
and ofcourse I can find all locations of each \"
and see which \" is most nearest after the location of <img src=\"
Get the length between and then substract the string from there so I get the substring to download the image and store it and rewrite it to where I'm storing it locally.
However I'm sure this can be done a lot easier:
Here is my code for now: (not fully done yet)
I'm trying to store a few pages locally so I can still load them up in my webview when I don't have a network connection.
Now the saving goes well, only problem I'm facing is with images.
I'm trying to get the content between <img src=\" and \"
So I can store this image locally as well and rewrite the code a little bit.
I can find all the locations of each <img src=\"
and ofcourse I can find all locations of each \"
and see which \" is most nearest after the location of <img src=\"
Get the length between and then substract the string from there so I get the substring to download the image and store it and rewrite it to where I'm storing it locally.
However I'm sure this can be done a lot easier:
Here is my code for now: (not fully done yet)
Code:
NSMutableArray *startRanges = [[NSMutableArray alloc] initWithObjects: nil];
NSMutableArray *endRanges = [[NSMutableArray alloc] initWithObjects: nil];
NSUInteger length = [pageHTMLstring length];
NSRange range = NSMakeRange(0, length);
while(range.location != NSNotFound)
{
range = [pageHTMLstring rangeOfString: @"<img src=\"" options:0 range:range];
if(range.location != NSNotFound)
{
range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
[startRanges addObject: [NSString stringWithFormat: @"%i", range.location]];
}
}
range = NSMakeRange(0, length);
while(range.location != NSNotFound)
{
range = [pageHTMLstring rangeOfString: @"\"" options:0 range:range];
if(range.location != NSNotFound)
{
range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
[endRanges addObject: [NSString stringWithFormat: @"%i", range.location]];
}
}
for(int a=0; a<[startRanges count]; a++) {
int end = 0;
for(int b=0; b<[endRanges count]; b++) {
if([[endRanges objectAtIndex: b] intValue] > [[startRanges objectAtIndex: a] intValue]) {
end = b;
break;
}
}
double start = [[startRanges objectAtIndex: a] intValue];
double stop = [[endRanges objectAtIndex: end] intValue];
NSString *test = [pageHTMLstring substringWithRange:NSMakeRange(start, (stop - start))];
NSLog(@"The image: %@",test);
}
Last edited: