【问题标题】:How to use SDWebImage in UIWebview如何在 UIWebview 中使用 SDWebImage
【发布时间】:2014-02-16 19:17:13
【问题描述】:

就像我们在UIImageview 中使用SDWebImage 设置图像

[imageview.setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                      completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {... completion code here ...}];

有什么办法可以在<img>标签的UIWebview中使用SDWebImage

NSString *htmlString =@"<html lang=\"en\"><img src='http://cdn.tutsplus.com/mobile/uploads/legacy/iOS-SDK_UIView-Animation/Animate-Icon.png'   /> </div><div id=\"content\"><div>BlahBlahBlah LoremIpsum</div><br></body>"

[WebView loadHTMLString:descriptionHT baseURL:nil];

提前致谢:)

【问题讨论】:

    标签: ios ios7 uiwebview sdwebimage


    【解决方案1】:

    首先缓存你的图片:

    SDWebImageManager *manager = [SDWebImageManager sharedManager];
      [manager downloadWithURL:[NSURL URLWithString:@"http://cdn.tutsplus.com/mobile/uploads/legacy/iOS-SDK_UIView-Animation/Animate-Icon.png"]
    options:
      0 progress : ^(NSInteger receivedSize, NSInteger expectedSize) {
        // progression tracking code
      }
    completed:
      ^(UIImage * image, NSError * error, SDImageCacheType cacheType, BOOL finished) {
        if (image && finished) {
          [[SDImageCache sharedImageCache] storeImage:image forKey:@"icon"];
        }
      }];
    

    然后当你需要图像时

    __block NSString *imageSource;
    __block NSData *imageData = [NSData data];
    
    SDImageCache *imageCache = [SDImageCache sharedImageCache];
    [imageCache queryDiskCacheForKey:@"icon" done:^(UIImage * image, SDImageCacheType cacheType) {
    imageData = UIImagePNGRepresentation(image);
       if (image) {
          imageSource = [NSString stringWithFormat:@"data:image/png;base64,%@", [imageData base64Encoding]];
       } else {
          //start download of image
       }
      }];
    

    然后在你的html中:

    NSString *htmlString = [NSString stringWithFormat:@"<html lang=\"en\"><img src=\"%@\"/> </div><div id=\"content\"><div>BlahBlahBlah LoremIpsum</div><br></body>", imageSource];
    
    [WebView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
    

    缓存图像时您不必使用密钥,我只是认为它使事情变得更容易一些。

    【讨论】:

      猜你喜欢
      • 2014-04-15
      • 2023-04-06
      • 2016-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多