2011-08-09 7 views
0

誰もがthis postで私を指摘する前に、私はすでにそれを試して、それは私のために動作しません。 MKMapViewの白黒スナップショットを生成しようとしています。しかし、品質は、iPhone 4では非常に低いです。ここに私のコードです。誰でも何か提案がありますか?低品質MKMapView動的に生成された画像

- (void)mapSnapShotWithMapView:(MKMapView *)_mapView { 
    CGSize s = CGSizeMake(_mapView.bounds.size.width, _mapView.bounds.size.height); 
    UIGraphicsBeginImageContextWithOptions(s, NO, 0.0f); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    [[_mapView layer] renderInContext:ctx]; 
    UIImage *thumb = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); 
    ctx = CGBitmapContextCreate(nil, s.width, s.height, 8, s.width, colorSpace, kCGImageAlphaNone); 
    CGContextSetShouldAntialias(ctx, YES); 
    CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh); 

    CGContextDrawImage(ctx, CGRectMake(0, 0, s.width, s.height), thumb.CGImage); 
    CGImageRef bwImage = CGBitmapContextCreateImage(ctx); 
    CGContextRelease(ctx); 
    CGColorSpaceRelease(colorSpace); 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, s.width, s.height)]; 
    [imageView setImage:[UIImage imageWithCGImage:bwImage]]; 
    CGImageRelease(bwImage); 
    [self.view addSubView:imageView]; 
    [imageView release]; 
} 

答えて

1

this postのおかげでわかりました。 UIGraphicsBeginImageContextWithOptionsの最後のパラメータが0.0fであっても、iPhoneのネイティブ解像度は使用されていませんでした。それにかかわらず、更新されたコードは次のとおりです。

- (void)mapSnapShotWithMapView:(MKMapView *)_mapView { 
    CGSize s = _mapView.bounds.size; 
    CGFloat scale = 1.0; 
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
    scale = [[UIScreen mainScreen] scale]; 
    s = CGSizeApplyAffineTransform(s, CGAffineTransformMakeScale(scale, scale)); 
    } 

    UIGraphicsBeginImageContextWithOptions(s, NO, 1.0f); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextScaleCTM(ctx, scale, scale); 
    [[_mapView layer] renderInContext:ctx]; 
    UIImage *thumb = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); 
    ctx = CGBitmapContextCreate(nil, s.width, s.height, 8, s.width, colorSpace, kCGImageAlphaNone); 
    CGContextSetShouldAntialias(ctx, YES); 
    CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh); 

    CGContextDrawImage(ctx, CGRectMake(0, 0, s.width, s.height), thumb.CGImage); 
    CGImageRef bwImage = CGBitmapContextCreateImage(ctx); 
    CGContextRelease(ctx); 
    CGColorSpaceRelease(colorSpace); 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, s.width/2, s.height/2)]; 
    [imageView setImage:[UIImage imageWithCGImage:bwImage]]; 
    CGImageRelease(bwImage); 
    [self.view addSubview:imageView]; 
    [imageView release]; 
} 
関連する問題