低质量 MKMapView 动态生成的图像
在有人向我指出这篇文章之前,我已经尝试过了,而且它对我不起作用。我正在尝试生成 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];
}
Before anyone points me at this post, I've already tried it, and it doesn't work for me. I am trying to generate a black and white snapshot of an MKMapView. The quality, however, is terribly low on the iPhone 4. Here is my code. Anyone have any suggestions?
- (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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢这篇文章,我弄清楚了。它没有使用 iPhone 的本机分辨率,尽管 0.0f 作为 UIGraphicsBeginImageContextWithOptions 的最后一个参数应该可以做到这一点。无论如何,这是更新后的代码:
I figured it out thanks to this post. It was not using the iPhone's native resolution, even though 0.0f as the last parameter of UIGraphicsBeginImageContextWithOptions should do this. Regardless, here is the updated code: