Низкокачественное динамически генерируемое изображение MKMapView - PullRequest
0 голосов
/ 09 августа 2011

Прежде чем кто-нибудь укажет мне на этот пост , я уже попробовал его, и он не работает для меняЯ пытаюсь создать черно-белый снимок MKMapView.Качество на iPhone 4 ужасно низкое. Вот мой код.У кого-нибудь есть предложения?

- (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 Ответ

1 голос
/ 10 августа 2011

Я понял это благодаря этому посту . Он не использовал собственное разрешение iPhone, хотя 0.0f как последний параметр UIGraphicsBeginImageContextWithOptions должен сделать это. В любом случае, вот обновленный код:

- (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];
}
...