[英]Load images in to a ScrollView
嗨,將緩存在磁盤上的加載圖像 (1024x1024) 加載到 UIScrollView 需要一段時間(只是延遲但很麻煩)才能加載到 memory 中。 當使用大小為 668px × 445px 的圖像時,滯后是可以接受的。 當我使用 dispatch_async 時,我嘗試使用線程加載數據相同的結果
任何 ide 如何提高異步加載數據的性能?
-(void)setupImageView:(BMPhoto *)_photo {
methodStart = [[NSDate date] retain];
if ([[photo imageUrl] isEqualToString:[_photo imageUrl]])
return;
photo = _photo;
[imageView removeFromSuperview];
[imageView release];
imageView = nil;
self.zoomScale = 1.0;
BMCache *cache = [BMCache sharedCache];
if ([cache isFileCached:[photo imageUrl]]) {
[NSThread detachNewThreadSelector:@selector(loadImageFromCache:) toTarget:self withObject:[photo imageUrl]];
} else {
[NSThread detachNewThreadSelector:@selector(loadImageFrom:) toTarget:self withObject:[photo imageUrl]];
}
}
-(void)loadImageFromCache:(NSString *)url{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *image = [UIImage imageWithData:[[BMCache sharedCache] getCachedRemoteFile:url]];
[self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:NO];
[pool release];
}
-(void)loadImageFrom:(NSString *)url{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[photo imageUrl]]];
[[BMCache sharedCache] addRemoteFileToCache:url withData:imageData];
UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease];
[imageData release];
[self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:NO];
[pool release];
}
- (void)displayImage:(UIImage *)image {
NSDate *methodFinish1 = [NSDate date];
NSTimeInterval executionTime1 = [methodFinish1 timeIntervalSinceDate:methodStart];
NSLog(@"Thread Done time: %f", executionTime1);
if (image) {
//imageView.image = nil;
imageView = [[BMPhotoViewImageView alloc] initWithImage:image];
[self addSubview:imageView];
self.contentSize = [image size];
[self setMaxMinZoomScalesForCurrentBounds];
self.zoomScale = self.minimumZoomScale;
NSTimeInterval executionTime2 = [[NSDate date] timeIntervalSinceDate:methodFinish1];
NSLog(@"Load image time time: %f", executionTime2);
NSDate *methodFinish = [NSDate date];
NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
NSLog(@"Total time: %f", executionTime);
NSLog(@"-------------------------------------------");
}
}
線程完成時間:0.015229 加載圖像時間:0.009824
線程完成時間:0.025037 加載圖像時間:0.005314
線程完成時間:0.026063 加載圖像時間:0.004379
您可以嘗試...首先顯示imageView(方法displayImage)將一些小圖像狀態“加載”或活動指示器。 然后加載(使用線程)並將實際圖像設置為 imageView。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.