3

URLからの画像を使用してカスタムMKAnnotationViewを作成しようとしています。

デバイスにRetinaディスプレイがある場合、解像度が2倍になるため、MKAnnotationViewの画像がぼやけます。

画像がアプリからのものである場合、@ 2x画像(存在する場合)が読み込まれますが、たとえば次のようなURLから画像を設定した場合:

- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id ) annotation    
{
MKAnnotationView *customAnnotationView=[[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:nil];

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:@"http://www.interaction-design.org/images/icons/play-button-red-300x300.png"]];


UIImage *img = [UIImage imageWithData:imageData];
[customAnnotationView setImage:img ];
return customAnnotationView;
}

非常にピクセル化された網膜ディスプレイ上の画像が表示されます。

何をすべきかアドバイスはありますか?ありがとう

4

1 に答える 1

5

要点はこれです...

  1. サーバーに標準または@2xイメージを要求するために使用[[UIScreen mainScreen] scale]します。
  2. CGImageRefからを作成しますNSData
  3. CGImageRefを使用[[UIScreen mainScreen] scale]して、UIImage

これを行うためのコードは次のようになります。

NSString *imagePath = @"http://www.interaction-design.org/images/icons/play-button-red-300x300";
imagePath = [imagePath stringByAppendingString:[[UIScreen mainScreen] scale] > 1 ? @"@2x.png": @".png"];
NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:imagePath]];

CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)imageData);
CGImageRef imageRef = CGImageCreateWithPNGDataProvider(dataProvider, NULL, NO, kCGRenderingIntentDefault);
UIImage *image = [UIImage imageWithCGImage:imageRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationUp];

CGDataProviderRelease(dataProvider);
CGImageRelease(imageRef);

コードの最初の2行を指定すると、サーバー上に画像の2つの解像度が必要になることに注意してください。現在、画像は網膜では小さく表示され、網膜以外の画面では大きく表示されます。

于 2012-08-20T07:37:51.067 に答える