2014-01-25 43 views
2

就像我們使用SDWebImage如何使用SDWebImage在UIWebView的

[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 ...}]; 

UIImageview設定圖像有什麼辦法在<img>標籤使用SDWebImage在UIWebView中提前

NSString *htmlString [email protected]"<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]; 

謝謝:)

回答

0

首先緩存您的圖片:

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]]]; 

您沒有緩存的圖像時使用的關鍵,我只是覺得這做事情更容易一點。