0
在我的應用程序中,用戶可以下載大文件(1GB +)。我有自己的進度視圖來表示下載進度。而我使用AFNetworking
這樣AFNetworking +進度視圖+效果
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead){
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
double progress = (double)(size + totalBytesRead)/(size + totalBytesExpectedToRead);
[[NSNotificationCenter defaultCenter] postNotificationName:ETBDownloadManagerProgressNotification object:component userInfo:@{ ETBDownloadProgressKey : [NSNumber numberWithDouble:progress], ETBDownloadExpectedSizeKey : [NSNumber numberWithDouble:(size + totalBytesExpectedToRead)*1.0/1024.0/1024.0] }];
}];
我有一個問題與性能,同時下載文件。同時可以只下載一個文件。時間事件探查器顯示我更新UI並經常致電drawRect:
。因此,這裏是我的發展觀setProgress:
- (void)setProgress:(double)progress
{
_progress = progress;
if (_progress > 1)
_progress = 1;
self.textLabel.text = [NSString stringWithFormat:@"%.f%%", 100*self.progress];
[self setNeedsDisplay];
}
而且發展觀的drawRect:
- (void)drawRect:(CGRect)rect
{
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath addArcWithCenter:CGPointMake(rect.size.width/2, rect.size.height/2)
radius:rect.size.width/2 - kLineWidth/2 - kPadding
startAngle:0
endAngle:2*M_PI
clockwise:YES];
bezierPath.lineWidth = kLineWidth;
[[UIColor colorWithRed:1 green:1 blue:1 alpha:.3] setStroke];
[bezierPath stroke];
bezierPath = [UIBezierPath bezierPath];
[bezierPath addArcWithCenter:CGPointMake(rect.size.width/2, rect.size.height/2)
radius:(rect.size.width/2 - kLineWidth - kPadding)/2
startAngle:startAngle
endAngle:(endAngle - startAngle) * self.progress + startAngle
clockwise:YES];
bezierPath.lineWidth = rect.size.width/2 - kLineWidth - kPadding;
[[UIColor colorWithRed:1 green:1 blue:1 alpha:.3] setStroke];
[bezierPath stroke];
}
的問題是如何避免落後?我發現的唯一方法是減少用於更新UI的postNotification
的數量。但是如何?我試圖使用setDownloadProgressBlock
裏面的if (i++%3 == 0)
這樣的條件,但是很醜陋:) 我也試着用self.textLabel.text = ...
替換成drawRect:
,但它沒有任何意義。 也許可以設置下載包的大小?
不錯,你把我的情況改成了漂亮的,謝謝 –
不客氣;) –