0

I have HTML code. I want to preload images from it and save in local file. I transform HTML to NSAttributedString and show that string in UITextView.

NSAttributedString *htmlAttributedText =
        [[NSAttributedString alloc] initWithData:[htmlText dataUsingEncoding:NSUTF16StringEncoding]
                                        options:@{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType }
                              documentAttributes:nil
                                           error:&err];

textView.attributedText = htmlAttributedText;

How to load images from local file instead of loading from internet?

rostislawk
  • 61
  • 1
  • 4

2 Answers2

2

Below code will list an array of images in your HTML. Get the list of all images

NSMutableArray * arrayToStoreImages = [[NSMutableArray alloc] init];
NSRegularExpression* imageRegex = [[NSRegularExpression alloc] initWithPattern:@"<img[^>]*>" options:0 error:NULL] ;
[imageRegex enumerateMatchesInString:HTMLBODY options:0 range:NSMakeRange(0, [HTMLBODY length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){

[arrayToStoreImages 
addObject:NSStringFromRange(match.range)];           
}];

Download all images using SDWebImage and store to local storage with name using HASH from fetched URL. Replace the actual URL to local HASHed path. Convert HTML to NSAttributedString and delete the image from local storage as it was temp.

If images is cached by SDWebImage it will be delivered from cache else it will download fresh one.

GVyas
  • 27
  • 6
0

If i understand you right, as for me simplest and fastest way to use SDWebImage

after you once downloaded you image, lib save it and next time when you try to download image from the same url lib will give you cached image.

Roman Simenok
  • 530
  • 7
  • 22
  • I think problem in following: when I try to get NSAttributedString from html, image will be loaded in some native way, and I don't know, how to force parser to get images from disk cache – rostislawk Apr 01 '15 at 13:07