2011-12-09 14 views
0

地図を表示する画像を返すMapServerから読み込まれたスクロールビューに複数の画像を表示する作業をしています。 私がしたことは、4つのUIImageViewを作成してNSMutableDictionaryに入れたことです。 その後、望ましいImageにスクロールすると、URL非同期からデータをロードするようになります。 最初にUIActivityIndi​​catorViewを表示し、次にデータをロードし、最後にUIActivityIndi​​catorViewを非表示にしてUIImageViewを表示します。iPhone UIImageViewは、setImageの表示を遅らせます

画像が大きく表示されない限り、画像が表示されずに機能が終了したことを示すログテキストが表示されるまで、すぐに表示されますが、画像はまだ表示されません。 ウェブブラウザで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を上書きしたカテゴリです。プレースホルダーの画像と、UIImageViewへのURLに言及する必要があります。一度ダウンロードされるとイメージが表示され、ダウンロードされたイメージのキャッシュも維持され、複数のサーバーコールが回避されます。

+0

ありがとうございます!私はこれについて忘れてしまった。私は既に他のプロジェクトでそれを使用している。あなたは自分ですべてを再構築するのではなく、誰かがすでに実装したものを使うことができます。ありがとうございました! – schurtertom

関連する問題