简体   繁体   中英

MKMapView renderInContext - iPhone 4 (Retina) issue

I am having a MKMapView of size 64x64. I am creating an image from map view using 'renderInContext:' method. And assigning the image to UITableViewCell's imageView. In normal iPhone, its showing the image correct. But in iPhone(Retina) its showing the image blurred. I am tesing this in simulator, not in actual device. I have attached the screen shots below.

替代文字

The thumbnail in the left side, with rounded corners is the UITableViewCell's imageView. The thumbnail in the right side is MKMapView.

I am using the following code to get the image from map view.

UIGraphicsBeginImageContext(CGSizeMake(64, 64));
CGContextRef context = UIGraphicsGetCurrentContext();
[[mapView layer] renderInContext:context];
thumbnail_image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Why this happens in iPhone(Retina)? How to fix this?

As already mentioned in a previous answer I gave :

Use UIGraphicsBeginImageContextWithOptions instead of UIGraphicsBeginImageContext :

UIGraphicsBeginImageContextWithOptions(CGSizeMake(64, 64), NO, 0.0);

See QA1703 for more details. It says:

Note: Starting from iOS 4, UIGraphicsBeginImageContextWithOptions allows you to provide with a scale factor. A scale factor of zero sets it to the scale factor of the device's main screen. This enables you to get the sharpest, highest-resolustion snapshot of the display, including a Retina Display.

could you please provide some sample code (because I'm really interested how you done that in fact it's new for me ^^)
And what I've done in a comparable situation I create an image with double the size and then let it shrink down automatically by UIImageView (setting the resize properties to fit all borders)

EDIT: but I think it should make the ScaleToFill automatically

UIGraphicsBeginImageContext(CGSizeMake(128, 128));    
…
myImageView.frame = CGRectMake(0, 0, 64, 64);
myImageView.contentMode = UIViewContentModeScaleToFill;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM