我有一个视频应用,使用NSURLConnection下载并保存许多视频。
我想知道是否有任何代码检查是否有足够的内存来保存所有这些视频。
以下是我用于下载视频的代码:
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:videoUrl]cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
if (theConnection) {
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
expectedLength = [response expectedContentLength];
currentLength = 0;
HUD.mode = MBProgressHUDModeDeterminate;
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.
// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
// receivedData is an instance variable declared elsewhere.
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
currentLength += [data length];
HUD.progress = currentLength / (float)expectedLength;
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
[HUD hide:YES];
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"The Internet connection appears to be offline." message:nil delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
NSLog(@"Documents directory not found!");
}
NSString *file2 = [NSString stringWithFormat:@"%@/%@.mp4",documentsDirectory,VideoName];
[receivedData writeToFile:file2 atomically:YES];
NSLog(@" %@ file 2 ",file2);
HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick.png"]] autorelease];
HUD.mode = MBProgressHUDModeCustomView;
[HUD hide:YES afterDelay:2];
// release the connection, and the data object
[connection release];
[receivedData release];
}
任何人都可以帮助我。谢谢。
答案 0 :(得分:1)
是的,您可以使用NSFileManager
获取可用空间。
NSError *err;
NSFileManager *fman = [NSFileManager defaultManager];
NSString *path = (NSString *)[[[fman URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] path];
NSDictionary *fattr = [fman attributesOfFileSystemForPath:path error:&err];
//Error checking
NSUInteger freeSize = [[fattr objectForKey:NSFileSystemFreeSize] unsignedIntegerValue];
NSLog(@"Free Space: %f Megabytes", ((float)freeSize)/1048576.00000000f);
答案 1 :(得分:0)
您可以在下载之前打开名为file2的文件。通过预期的文件大小+额外的一点来查找文件并保存/关闭它。
然后当你做
时[receivedData writeToFile:file2 atomically:YES];
应该预先分配空间。如果它最初因为没有足够的空间而失败,你可以不用下载。