【问题标题】:NSURLRequest get pdf sizeNSURLRequest 获取 pdf 大小
【发布时间】:2011-12-16 12:08:48
【问题描述】:
我有一个使用 NSURLRequest 加载 PDF 的 UIWebview。它将加载 PDF,但如果 pdf 太大,应用程序会崩溃。我没有收到内存警告。有没有办法在下载之前获取 PDF 的大小?
这是我的加载方式:
NSURLRequest *currentRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:myUrlString]];
【问题讨论】:
标签:
objective-c
uiwebview
ios5
【解决方案1】:
我认为你应该尝试使用 NSURLMutableRequest 来代替下载大文件,这样你就可以获得多部分下载。您还可以使用 NSURLResponse 的 expectedContentLength 方法来了解文件长度。这是一个例子:
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse*)response {
NSLog (@"%d", [response expectedContentLength]);
}
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data {
if (self.currentDownloadedFile) {
[self.currentDownloadedFile seekToEndOfFile];
[self.currentDownloadedFile writeData:data];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection*)connection {
// Close the file here
}
并强制 UIWebView 打开下载的文件,而不是从 un URL 下载。