Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iphone中下载图像时在UIProgressView中显示准确的进度_Iphone_Ios_Objective C_Xcode_Ipad - Fatal编程技术网

在iphone中下载图像时在UIProgressView中显示准确的进度

在iphone中下载图像时在UIProgressView中显示准确的进度,iphone,ios,objective-c,xcode,ipad,Iphone,Ios,Objective C,Xcode,Ipad,我有四个由图像组成的URL。。。 我正在下载这些图像并将其放入文档文件夹。 -(void)viewDidLoad { NSMutableArray *myUrlsArray=[[NSMutableArray alloc]init]; [myUrlsArray addObject:@"http://blogs.sfweekly.com/thesnitch/steve_jobs3.jpg"]; [myUrlsArray addObject:@"http://www.droid-life.com/wp

我有四个由图像组成的URL。。。 我正在下载这些图像并将其放入文档文件夹。

-(void)viewDidLoad
{
NSMutableArray *myUrlsArray=[[NSMutableArray alloc]init];
[myUrlsArray addObject:@"http://blogs.sfweekly.com/thesnitch/steve_jobs3.jpg"];
[myUrlsArray addObject:@"http://www.droid-life.com/wp-content/uploads/2012/12/Steve-Jobs-Apple.jpg"];
[myUrlsArray addObject:@"http://2.bp.blogspot.com/-T6nbl0rQoME/To0X5FccuCI/AAAAAAAAEZQ/ipUU7JfEzTs/s1600/steve-jobs-in-time-magazine-front-cover.png"];
[myUrlsArray addObject:@"http://images.businessweek.com/ss/08/09/0929_most_influential/image/steve_jobs.jpg"];
[myUrlsArray addObject:@"http://cdn.ndtv.com/tech/gadget/image/steve-jobs-face.jpg"];

for (int i=0; i<myUrlsArray.count; i++)
{
    [self downloadImageFromURL:[myUrlsArray objectAtIndex:i] withName:[NSString stringWithFormat:@"MyImage%i.jpeg",i]];
}

}




#pragma mark- downloading File

-(void)downloadImageFromURL:(NSString *)myURLString withName:(NSString *)fileName
{
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:myURLString]]];

NSLog(@"%f,%f",image.size.width,image.size.height);

   // Let's save the file into Document folder.**

    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];

NSString *jpegPath = [NSString stringWithFormat:@"%@/%@",documentsPath,fileName];// this path if you want save reference path in sqlite
NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality
[data2 writeToFile:jpegPath atomically:YES];

}
这是我的代码。

-(void)viewDidLoad
{
NSMutableArray *myUrlsArray=[[NSMutableArray alloc]init];
[myUrlsArray addObject:@"http://blogs.sfweekly.com/thesnitch/steve_jobs3.jpg"];
[myUrlsArray addObject:@"http://www.droid-life.com/wp-content/uploads/2012/12/Steve-Jobs-Apple.jpg"];
[myUrlsArray addObject:@"http://2.bp.blogspot.com/-T6nbl0rQoME/To0X5FccuCI/AAAAAAAAEZQ/ipUU7JfEzTs/s1600/steve-jobs-in-time-magazine-front-cover.png"];
[myUrlsArray addObject:@"http://images.businessweek.com/ss/08/09/0929_most_influential/image/steve_jobs.jpg"];
[myUrlsArray addObject:@"http://cdn.ndtv.com/tech/gadget/image/steve-jobs-face.jpg"];

for (int i=0; i<myUrlsArray.count; i++)
{
    [self downloadImageFromURL:[myUrlsArray objectAtIndex:i] withName:[NSString stringWithFormat:@"MyImage%i.jpeg",i]];
}

}




#pragma mark- downloading File

-(void)downloadImageFromURL:(NSString *)myURLString withName:(NSString *)fileName
{
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:myURLString]]];

NSLog(@"%f,%f",image.size.width,image.size.height);

   // Let's save the file into Document folder.**

    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];

NSString *jpegPath = [NSString stringWithFormat:@"%@/%@",documentsPath,fileName];// this path if you want save reference path in sqlite
NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality
[data2 writeToFile:jpegPath atomically:YES];

}
-(void)viewDidLoad
{
NSMutableArray*myUrlsArray=[[NSMutableArray alloc]init];
[myUrlsArray addObject:@”http://blogs.sfweekly.com/thesnitch/steve_jobs3.jpg"];
[myUrlsArray addObject:@”http://www.droid-life.com/wp-content/uploads/2012/12/Steve-Jobs-Apple.jpg"];
[myUrlsArray addObject:@”http://2.bp.blogspot.com/-T6nbl0rQoME/To0X5FccuCI/AAAAAAAAEZQ/ipUU7JfEzTs/s1600/steve-jobs-in-time-magazine-front-cover.png"];
[myUrlsArray addObject:@”http://images.businessweek.com/ss/08/09/0929_most_influential/image/steve_jobs.jpg"];
[myUrlsArray addObject:@”http://cdn.ndtv.com/tech/gadget/image/steve-jobs-face.jpg"];

对于(int i=0;i您对dataWithContentsOfURL的调用是同步的,这意味着您在下载过程中不会得到更新


您可以使用像AFNetworking()这样的库,它可以回调下载进度。

实际上更好的解决方案是使用SDWebImage manager,它将在后台为您加载图像并缓存它们。然后下次使用该图像时,它将检查缓存。用谷歌搜索它

这样用户就不必在你下载东西的时候坐在那里等待

然后再看另一个问题,它对如何进行身份认证有一些想法:


不要将
数据与URL的内容一起使用,在数据到达之前,您将阻塞主线程

而是使用
NSURLConnection
创建自己的连接,并开始聆听您的学员的讲话

  • 连接:(NSURLConnection*)连接didReceiverResponse:(nsurResponse*)响应
    :使用
    [response expectedContentLength]
    获取总数据大小
  • connection:(NSURLConnection*)connection did接收数据:(NSData*)数据
    :这是您进行计算和更新
    UIProgressView
    的地方。类似于loadedBytes/总数据大小

祝你好运。

我建议你使用一些异步下载技术(AFNetworking、SDWebImage,或者使用基于代理的
NSURLSession
)而不是
dataWithContentsOfURL
,这样(a)你就不会阻塞主队列;(b)你可以在下载过程中获得进度更新

我还建议为每次下载创建一个代理。当您的委托方法获得有关已下载字节数的更新时,更新
NSProgress
对象

然后,您可以将每个
NSProgress
与a相关联,并在更新
NSProgress
时自动更新UI

或者,如果您和单个
UIProgressView
显示每次下载的所有
NSProgress
的聚合进度,您可以创建一个父
NSProgress
,将每次下载的
NSProgress
建立为父
NSProgress
的子级,然后在每次下载时更新其各自的
NSProgress
,这将自动触发父进程的计算。同样,您可以将父进程绑定到主进程上,只需让每次下载更新其各自的
NSProgress
,就可以用总进程自动更新UI


不过,有一个技巧是,某些web服务不会通知您预期的字节数。它们将报告
nsurresponseunknownlength
的“预期字节数”,即-1!(它这样做的逻辑原因可能超出了这个问题的范围。)这显然使计算下载的百分比变得困难

在这种情况下,有几种方法:

  • 你可以举手,只用一个不确定的进度指标

  • 您可以尝试更改请求,以便web服务报告有意义的“预期字节数”值(例如);或

  • 您可以使用“估计下载大小”来估计完成百分比。例如,如果您知道您的图像平均每个100kb,您可以执行以下操作来更新与特定下载相关的
    NSProgress

    if (totalBytesExpectedToWrite >= totalBytesWritten) {
        self.progress.totalUnitCount = totalBytesExpectedToWrite;
    } else {
        if (totalBytesWritten <= 0) {
            self.progress.totalUnitCount = kDefaultImageSize;
        } else {
            double written = (double)totalBytesWritten;
            double percent = tanh(written / (double)kDefaultImageSize);
            self.progress.totalUnitCount = written / percent;
        }
    }
    
    self.progress.completedUnitCount = totalBytesWritten;
    
    if(totalBytesExpectedToWrite>=TotalBytesWrite){
    self.progress.totalUnitCount=totalBytesExpectedToWrite;
    }否则{
    
    如果(TotalBytes为这个东西写的首先,在完全下载之前,你必须知道图像的总大小,然后你每次都必须计算下载的数据,以及该数据的百分比w.r.t总大小,只有我认为这是可能的。顺便说一句,你真的不想下载
    NSData
    ,创建一个
    UIImage
    ,然后使用
    UIImageJPEGRepresentation
    重新提取
    NSData
    。JPG是有损的,因此您将失去质量,或者如果使用1.0提高质量,文件无疑会比原始文件大。如果您自己这样做,请确保将原始
    NSData
    检索到它自己的变量中,并且你想保存它,保存它,而不是
    uiimagejpegresentation
    的结果。这是一个黑客行为,所以我不想列出它作为答案。但在这种情况下,我做的是建立我自己的进度条,并在每个图像完成时更新25%。实际上,为了让它看起来不只是在75%时停止然后消失,我将它设置为90%在删除它之前做一些其他的快速任务。谢谢@sanjitshaw…@Rob…@badweasel..再次感谢您花费宝贵的时间为我的问题提供了一个好的详细的答案…您的答案帮了我很多忙…嘿@Rob。我在[\uu NSCFString totalBytesExpected]收到崩溃消息:未识别的选择器发送到实例