2011-07-14 24 views
15

Tôi đang làm việc trên trang hỗn hợp văn bản hình ảnh trên iOS. Tôi biết tôi có thể sử dụng UIWebView để đạt được mục tiêu. Nhưng vấn đề là, người dùng có thể cần đọc trang ngoại tuyến. Đối với phần văn bản, tôi có thể lưu html vào đĩa và tải nó ở chế độ ngoại tuyến. Nhưng làm thế nào về hình ảnh? Có thể lưu hình ảnh vào đĩa và UIWebView vẫn có thể hiển thị chúng không?cách lưu toàn bộ trang web bằng hình ảnh trong iOS

Cảm ơn bạn!

Trả lời

15

ASIHTTPRequest project có một lớp được gọi là ASIWebPageRequest được thiết kế để thực hiện chính xác những gì bạn muốn. Nếu bạn đồng ý với việc thêm một sự phụ thuộc bổ sung vào dự án của bạn thì tôi nghĩ đó là một giải pháp tốt cho bạn: ASIWebPageRequest.

Trên trang tôi thích ở trên có một số ví dụ tốt về làm thế nào để sử dụng nó nhưng tôi sẽ bao gồm một trong số họ ở đây cho đầy đủ:

- (IBAction)loadURL:(NSURL *)url 
{ 
    // Assume request is a property of our controller 
    // First, we'll cancel any in-progress page load 
    [[self request] setDelegate:nil]; 
    [[self request] cancel]; 

    [self setRequest:[ASIWebPageRequest requestWithURL:url]]; 
    [[self request] setDelegate:self]; 
    [[self request] setDidFailSelector:@selector(webPageFetchFailed:)]; 
    [[self request] setDidFinishSelector:@selector(webPageFetchSucceeded:)]; 

    // Tell the request to embed external resources directly in the page 
    [[self request] setUrlReplacementMode:ASIReplaceExternalResourcesWithData]; 

    // It is strongly recommended you use a download cache with ASIWebPageRequest 
    // When using a cache, external resources are automatically stored in the cache 
    // and can be pulled from the cache on subsequent page loads 
    [[self request] setDownloadCache:[ASIDownloadCache sharedCache]]; 

    // Ask the download cache for a place to store the cached data 
    // This is the most efficient way for an ASIWebPageRequest to store a web page 
    [[self request] setDownloadDestinationPath: 
     [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:[self request]]]; 

    [[self request] startAsynchronous]; 
} 

- (void)webPageFetchFailed:(ASIHTTPRequest *)theRequest 
{ 
    // Obviously you should handle the error properly... 
    NSLog(@"%@",[theRequest error]); 
} 

- (void)webPageFetchSucceeded:(ASIHTTPRequest *)theRequest 
{ 
    NSString *response = [NSString stringWithContentsOfFile: 
     [theRequest downloadDestinationPath] encoding:[theRequest responseEncoding] error:nil]; 
    // Note we're setting the baseURL to the url of the page we downloaded. This is important! 
    [webView loadHTMLString:response baseURL:[request url]]; 
} 
+0

Cảm ơn nhiều cho mẫu này, sẽ cố gắng nó sau này. Bạn có thể cho tôi biết ASIReplaceExternalResourcesWithData có thể nhúng dữ liệu vào trang không? Trang vẫn là một tệp html trong bộ nhớ cache, phải không? Yêu cầu có sửa đổi html gốc không? –

+0

Đúng, bạn nói đúng. ASI sửa đổi trang và thay thế các tài sản đó bằng [URI dữ liệu] (http://en.wikipedia.org/wiki/Data_URI_scheme). Ngoài ra, bạn có thể sử dụng một chế độ thay thế URL khác được gọi là 'ASIReplaceExternalResourcesWithLocalURLs' sẽ tải xuống tài nguyên dưới dạng tệp nhưng vẫn sửa đổi HTML để trỏ vào các tệp cục bộ này. – xoebus

+0

Cảm ơn xoebus. Tôi cũng phát hiện ra rằng để kích hoạt tính năng duyệt ngoại tuyến, tôi cần phải thêm dòng mã sau: 'request.cachePolicy = ASIAskServerIfModifiedCachePolicy | ASIFallbackToCacheIfLoadFailsCachePolicy;' –

Các vấn đề liên quan