如何从UIWebView(或WKWebView)访问资产目录中的图像资源

我们在UIWebView显示了一个本地html。 在那里,我们想要显示资产目录中定义的图像。

我知道如果我们在主要包中保持图像平坦,我们可以做类似的事情。 代码片段就是这样的。

 webView.loadHTMLString("", baseURL: NSBundle.mainBundle().bundleURL) 

但是,如果png文件打包在资产目录中,我不确定我可以为“https://stackoverflow.com/questions/26920757/how-to-access-image-resource-in-asset-catalog-from-uiwebview-or-wkwebview/target_image.png”指定什么。 (此外,我们要指定pdf以利用Xcode 6中的矢量图像支持)

有谁知道如何实现这一目标?

由于资产目录作为单个文件存储在捆绑包中,因此无法获取单个资产的URL。

最简单的解决方案是不要将资产目录用于您将在Web视图中显示的图像。

如果将图像保留在资产目录中是必不可少的,那么您唯一的选择是将所需资产“解包”到不同的文件到应用程序包的文档目录中,然后引用这些文件。

有一种方法可以从UIWebView访问资产目录中的图像资源。 您应该创建NSUrlProtocol子类,注册新协议并手动响应WebView图像请求。

 @interface AppDelegate : UIResponder  @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [NSURLProtocol registerClass:[ImageProtocol class]]; //... other code return YES; } @end @interface ImageProtocol : NSURLProtocol @end @implementation ImageProtocol + (BOOL)canInitWithRequest:(NSURLRequest *)request { NSString *imgName = (NSString *)[[request.URL.absoluteString componentsSeparatedByString:@"/"] lastObject]; AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate]; NSLog(@"imgName = %@",imgName); if ([UIImage imageNamed:imgName] != nil) {// or check for explicit name "target_image.png" return YES; } return NO; } + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request { return request; } + (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b{ return [super requestIsCacheEquivalent:a toRequest:b]; } - (void)startLoading { NSString *imgName = (NSString *)[[self.request.URL.absoluteString componentsSeparatedByString:@"/"] lastObject]; UIImage *img = [UIImage imageNamed:imgName]; NSString *mimeType = @"image/png"; @try { NSData *imageData = nil; if ([imgName hasSuffix:@".png"]) { imageData = UIImagePNGRepresentation(img); } else if ([imgName hasSuffix:@".jpg"]) { imageData = UIImageJPEGRepresentation(img, 1); mimeType = @"image/jpg"; } NSString *encoding = @"utf-8"; NSURLResponse *response = [[NSURLResponse alloc] initWithURL:self.request.URL MIMEType:mimeType expectedContentLength:imageData.length textEncodingName:encoding]; [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed]; [self.client URLProtocol:self didLoadData:imageData]; [self.client URLProtocolDidFinishLoading:self]; } @catch (NSException *exception) { NSError *err = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]; [self.client URLProtocol:self didFailWithError:err]; NSLog(@"%@",exception.debugDescription); } } - (void)stopLoading { // do nothing } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.client URLProtocol:self didLoadData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { [self.client URLProtocolDidFinishLoading:self]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [self.client URLProtocol:self didFailWithError:error]; } @end