2011-12-09 59 views
0

我正在从一个MapServer加载显示地图的图像显示在滚动视图中显示几个图像。 所以我做的是我创建了4个UIImageViews,把它们放在一个NSMutableDictionary中。 然后当滚动到所需的图像时,它将启动从异步URL加载数据。因此我首先显示一个UIActivityIndi​​catorView,然后它将加载数据并在最后隐藏UIActivityIndi​​catorView并显示UIImageView。iPhone UIImageView延迟显示时,setImage

一切工作或多或少,除了它需要很长的时间直到图像显示,所有虽然图像不是那么大,我有一个日志文本表明函数的结束到达...此日志消息立即出现,但图像仍然不显示... 如果我通过Web浏览器调用URL,图像也会立即显示。

下面你看到我的一段代码。

- (void) loadSRGImage:(int) page { 

UIImageView *currentSRGMap = (UIImageView *)[srgMaps objectForKey:[NSString stringWithFormat:@"image_%i", page]]; 
UIActivityIndicatorView *currentLoading = (UIActivityIndicatorView *)[srgMaps objectForKey:[NSString stringWithFormat:@"loading_%i", page]]; 


// if the image has been loaded already, do not load again 
if (currentSRGMap.image != nil) return; 

if (page > 1) { 

    MKCoordinateSpan currentSpan; 
    currentSpan.latitudeDelta = [[[srgMaps objectForKey:[NSString stringWithFormat:@"span_%i", page]] objectForKey:@"lat"] floatValue]; 
    currentSpan.longitudeDelta = [[[srgMaps objectForKey:[NSString stringWithFormat:@"span_%i", page]] objectForKey:@"lon"] floatValue]; 

    region.span   = currentSpan; 
    region.center  = mapV.region.center; 
    [mapV setRegion:region animated:TRUE]; 
    //[mapV regionThatFits:region]; 
} 
srgLegende.hidden = NO; 

currentSRGMap.hidden = YES; 
currentLoading.hidden = NO; 
[currentLoading startAnimating]; 

NSOperationQueue *queue = [NSOperationQueue new]; 
NSInvocationOperation *operation = [[NSInvocationOperation alloc] 
            initWithTarget:self 
            selector:@selector(loadImage:) 
            object:[NSString stringWithFormat:@"%i", page]]; 

[queue addOperation:operation]; 
[operation release]; 

}

- (void) loadImage:(NSInvocationOperation *) operation { 

NSString *imgStr = [@"image_" stringByAppendingString:(NSString *)operation]; 
NSString *loadStr = [@"loading_" stringByAppendingString:(NSString *)operation]; 

WGS84ToCH1903 *converter = [[WGS84ToCH1903 alloc] init]; 

CLLocationCoordinate2D coord1 = [mapV convertPoint:mapV.bounds.origin toCoordinateFromView:mapV]; 
CLLocationCoordinate2D coord2 = [mapV convertPoint:CGPointMake(mapV.bounds.size.width, mapV.bounds.size.height) toCoordinateFromView:mapV]; 
int x1 = [converter WGStoCHx:coord1.longitude withLat:coord1.latitude]; 
int y1 = [converter WGStoCHy:coord1.longitude withLat:coord1.latitude]; 
int x2 = [converter WGStoCHx:coord2.longitude withLat:coord2.latitude]; 
int y2 = [converter WGStoCHy:coord2.longitude withLat:coord2.latitude]; 

NSString *URL = [NSString stringWithFormat:@"http://map.ssatr.ch/mapserv?mode=map&map=import/dab/maps/dab_online.map&mapext=%i+%i+%i+%i&mapsize=320+372&layers=DAB_Radio_Top_Two", y1, x1, y2, x2]; 

NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:URL]]; 
UIImage* image = [[UIImage alloc] initWithData:imageData]; 
[imageData release]; 

UIImageView *currentSRGMap = (UIImageView *)[srgMaps objectForKey:imgStr]; 
UIActivityIndicatorView *currentLoading = (UIActivityIndicatorView *)[srgMaps objectForKey:loadStr]; 

currentSRGMap.hidden = NO; 
currentLoading.hidden = YES; 
[currentLoading stopAnimating]; 

[currentSRGMap setImage:image]; //UIImageView 
[image release]; 

NSLog(@"finished loading image: %@", URL); 

}

回答

0

我在我的应用程序也有类似的事情,我从https://github.com/rs/SDWebImage使用的SDWebImage。这有一个类别写在UIImageView。你需要提到一个占位符图片和一个URL到UIImageView。它会在下载完成后显示图像,并且还会保存已下载图像的缓存,从而避免多次服务器调用。

+0

非常感谢!我忘了这个..我已经在其他项目中使用过它。你是对的,而不是自己重建所有东西,我可以使用已经实现的人。谢谢! – schurtertom