3

URL에서 이미지로 사용자 지정 MKAnnotationView를 만들려고합니다.망막 디스플레이 용 MKAnnotationView에 URL의 이미지를 추가하는 방법은 무엇입니까?

장치에 망막 디스플레이가있을 때 MKAnnotationView의 이미지는 해상도가 두 배이므로 흐려집니다. 이미지가 응용 프로그램에서 경우 (있는 경우)

, 그것은 @ 2 배 이미지를로드합니다,하지만 당신은 예를 들어 다음과 같이 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; 
} 

당신이 표시됩니다 매우 pixelated 망막 디스플레이에 이미지.

어떤 조언을 원하십니까? 감사

답변

5

요점은 이것이다 ...

  1. 사용 [[UIScreen mainScreen] scale] 표준에 대해 서버를 요청하거나 배 이미지 @합니다.
  2. UIImage

에게 그 작업을 수행하는 코드를 작성하는 a를 CGImageRef NSData

  • 사용에서 CGImageRef[[UIScreen mainScreen] scale] 만들기 다음과 같습니다
    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); 
    

    그냥 당신이 필요 않습니다 내 코드의 처음 두 줄이 주어지면 서버에 이미지의 두 가지 해상도가 있습니다. 현재 이미지는 망막에서 더 작게 표시되고, 망막이 아닌 화면에서는 더 크게 표시됩니다.

  • +0

    여기서 작동하는 코드를 전달할 수 있습니까? 감사합니다 – ozba

    +0

    몇 가지 작업 코드 예제로 내 대답을 업데이 트했습니다. 5 번째 줄에 ** __ bridge ** cast가 ARC와 함께 작동해야하며 그렇지 않으면 필요하지 않습니다. – Jessedc

    +0

    감사합니다. 왜 특정 스케일 인자로 init에 해당하는 UIImage 메서드가 없는지 궁금합니다. – elsurudo

    관련 문제