【问题标题】:Upload asynchronous image with progress上传带有进度的异步图片
【发布时间】:2015-08-09 08:59:36
【问题描述】:

在我的应用程序中,我正在将图像上传到服务器。我正在使用这种方法https://stackoverflow.com/a/22301164/1551588。它工作正常,但我想为这个方法添加进度条。有可能的? sendAsynchronousRequest可以吗?感谢您的回复。

【问题讨论】:

  • 上传前是否将图片转换为 base 64 字符串?

标签: ios objective-c nsurlconnection nsurlrequest data-synchronization


【解决方案1】:

在 iOS 中似乎无法获取进度值。

但在 SO 上,我发现了一个很好的解决方法,他基本上是在作弊,但从视觉上看,他做到了。

您为自己填写一个进度指示器,最后确保它已满。

原答案UIWebView with Progress Bar

有改进的代码:

#pragma mark - Progress View

Boolean finish = false;
NSTimer *myTimer;

-(void)startProgressView{
    _progressView.hidden = false;
    _progressView.progress = 0;
    finish = false;
    //0.01667 is roughly 1/60, so it will update at 60 FPS
    myTimer = [NSTimer scheduledTimerWithTimeInterval:0.01667 target:self selector:@selector(timerCallback) userInfo:nil repeats:YES];
}
-(void)stopProgressView {
    finish = true;
    _progressView.hidden = true;
}

-(void)timerCallback {
    if (finish) {
        if (_progressView.progress >= 1) {
            _progressView.hidden = true;
            [myTimer invalidate];
        }
        else {
            _progressView.progress += 0.1;
        }
    }
    else {
        _progressView.progress += 0.00125;
        if (_progressView.progress >= 0.95) {
            _progressView.progress = 0.95;
        }
    }
    //NSLog(@"p %f",_progressView.progress);
}

这里如何使用它:

第一次调用(显然)你需要的地方

[self startProgressView];

然后在委托中

#pragma mark - NSURLConnection Delegate Methods

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"Loaded");
    [self stopProgressView];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error %@", [error description]);
    [self stopProgressView];
}

```

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-07
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多