[英]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.