2011-12-16 49 views
1

我有一個UIWebview加載PDF與NSURLRequest。它會加載PDF,但如果PDF太大,應用程序崩潰。我沒有收到內存警告。在下載之前有沒有辦法獲得PDF的大小?NSURLRequest獲取pdf大小

這是怎麼了加載它:

NSURLRequest *currentRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:myUrlString]]; 

回答

1

我想你應該嘗試的用戶NSURLMutableRequest相反在下載大文件,這樣你就可以得到多部分下載的情況。您也可以使用NSURLResponse的expectedContentLength方法來了解文件長度。下面是一個例子:

- (void)connection:(NSURLConnection *)connection 

didReceiveResponse:(NSURLResponse *)響應{

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下載。