Tôi muốn thêm vào câu trả lời @DevC, bằng cách cung cấp phương thức (có khối hoàn thành) để thêm chú thích ở giữa hình ảnh. Lúc đầu, tôi đã quan tâm đến việc xem liệu tôi có thể thêm chú thích trước khi ảnh chụp nhanh bắt đầu hay không, tuy nhiên MKMapSnapshotter
không not support this. Vì vậy, giải pháp tôi đưa ra, là tạo một ghim, vẽ nó lên trên hình ảnh bản đồ và sau đó tạo một hình ảnh khác của bản đồ + ghim. Đây là phương pháp tùy chỉnh của tôi:
-(void)create_map_screenshot:(MKCoordinateRegion)region :(map_screenshot_completion)map_block {
// Set the map snapshot properties.
MKMapSnapshotOptions *snap_options = [[MKMapSnapshotOptions alloc] init];
snap_options.region = region;
snap_options.size = // Set the frame size e.g.: custom_view.frame.size;
snap_options.scale = [[UIScreen mainScreen] scale];
// Initialise the map snapshot camera.
MKMapSnapshotter *map_camera = [[MKMapSnapshotter alloc] initWithOptions:snap_options];
// Take a picture of the map.
[map_camera startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
// Check if the map image was created.
if ((error == nil) && (snapshot.image != nil)) {
// Create the pin image view.
MKAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:nil];
// Get the map image data.
UIImage *image = snapshot.image;
// Create a map + location pin image.
UIGraphicsBeginImageContextWithOptions(image.size, YES, image.scale); {
[image drawAtPoint:CGPointMake(0.0f, 0.0f)];
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
// Create the pin co-ordinate point.
CGPoint point = [snapshot pointForCoordinate:region.center];
if (CGRectContainsPoint(rect, point)) {
// Draw the pin in the middle of the map.
point.x = (point.x + pin.centerOffset.x - (pin.bounds.size.width/2.0f));
point.y = (point.y + pin.centerOffset.y - (pin.bounds.size.height/2.0f));
[pin.image drawAtPoint:point];
}
}
// Get the new map + pin image.
UIImage *map_plus_pin = UIGraphicsGetImageFromCurrentImageContext();
// Return the new image data.
UIGraphicsEndImageContext();
map_block(map_plus_pin);
}
else {
map_block(nil);
}
}];
}
Phương pháp này cũng đòi hỏi một tiêu đề hoàn thành được khai báo như sau:
typedef void(^map_screenshot_completion)(UIImage *);
Tôi hy vọng đây là một số sử dụng cho những người tìm thêm chú thích cho hình ảnh bản đồ .
cảm ơn công việc của mình cho tôi và tiết kiệm thời gian của tôi. –
tôi có thể chỉnh sửa hình ảnh pin –