2011-12-09 57 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