Sar*_*aya 4 iphone xcode download file-handling ios
您好我想运行一个线程并检查当前下载的文件大小.
这就是我使用的
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://lasp.colorado.edu/home/wp-content/uploads/2011/03/suncombo1.jpg"]]];
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *jpegFilePath = [NSString stringWithFormat:@"%@/test.jpeg",docDir];
NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality
[data2 writeToFile:jpegFilePath atomically:YES];
downloadStatus.text =[NSString stringWithFormat:@"size: %zd", malloc_size(data2)];
[image release];
Run Code Online (Sandbox Code Playgroud)
我也尝试将malloc_size(data2)更改为图像,但同样不是真正的结果.我知道这没有线程,也没有在下载过程中检查但是我应该在这里使用什么来查看文件大小?
几点意见:
您的问题假定您尝试检索其大小的尝试NSData失败了.他们不是.获得a大小的正确方法NSData是通过length.
然而,你的困惑源于一个错误的假设,即在往返过程中采用外部生成的JPEG UIImage并UIImageJPEGRepresentation产生相同的结果NSData.这种情况极不可能发生.有太多不同的JPG设置可以更改(请参阅JPEG Wikipedia页面).我们当然不知道原始文件使用了什么设置.我知道UIImage和/或UIImageJPEGRepresentation改变了文件的颜色空间.我也打赌它还在做很多其他事情.
所以你的结果是正确的.原始文件是2.6mb,结果文件是4.5mb.如果将compressionQuality1.0从1.0 更改为0.99,则生成的文件仅为1.4mb!但如果您想要原始文件,请先保存(如下所示).
考虑下面的代码,它下载图像文件,保存,加载到a UIImage,通过重新提取UIImageJPEGRepresentation,并保存图像的另一个副本:
// let's make filenames where we'll store the files
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *suncomboOrig = [documentsPath stringByAppendingPathExtension:@"suncombo1-orig.jpg"];
NSString *suncomboReprocessed = [documentsPath stringByAppendingPathExtension:@"suncombo1-reprocessed.jpg"];
// let's download the original suncombo1.jpg and save it in Documents and display the size
NSURL *url = [NSURL URLWithString:@"http://lasp.colorado.edu/home/wp-content/uploads/2011/03/suncombo1.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSLog(@"original = %d", [data length]);
[data writeToFile:suncomboOrig atomically:NO];
// let's load that into a UIImage
UIImage *image = [UIImage imageWithData:data];
// let's extract data out of the image and write that to Documents, too, also logging the size of that
NSData *data2 = UIImageJPEGRepresentation(image, 1.0);
NSLog(@"reprocessed = %d", [data2 length]);
[data2 writeToFile:suncomboReprocessed atomically:NO];
Run Code Online (Sandbox Code Playgroud)
它的作用是报道:
2012-12-13 22:30:39.576 imageapp[90647:c07] original = 2569128 2012-12-13 22:30:40.141 imageapp[90647:c07] reprocessed = 4382876
所以我保存的第一个文件(我怀疑它与你服务器上的相同)是2.5mb,并且在进行了往返UIImage并通过4.3mb重新提取之后的文件.如果我查看上面代码保存的两个文件,我可以确认这些NSData大小是正确的.
我的原始答案是基于这样的假设,即OP无法检索a的大小,NSData或者在简单问题的基础上存在一些微妙的问题(例如想要在下载开始之前获得大小).无论如何,我已经扩展了上面的答案,但我将保留原始答案用于历史目的:
原答案:
该NSData属性length告诉您下载了多少字节.例如[data2 length].
如果它非常大,您可以使用NSURLConnection异步下载,根据您的Web服务器,可以在方法下载开始之前提供总文件大小didReceiveResponse(参数中包含expectedContentLength属性NSHTTPURLResponse *response).
NSURLConnection下载的另一个好处是,您不必在下载时将整个文件加载到内存中,而是可以将其直接流式传输到持久存储,这在您下载多个大文件时尤其有用同时.如果你正在下载一个合理大小的文件,使用NSURLConnection下载是过度的,但是在下载大文件并且你想要一个进度指示器(或者想要在下载开始之前获得文件大小)时它会很好.
但是,如果您只想知道下载了多少字节NSData,请使用length.
| 归档时间: |
|
| 查看次数: |
3043 次 |
| 最近记录: |