ios - 拍照并将其用作 map View 上的图钉

标签 ios mkmapview uiimagepickercontroller mkannotationview

几天来,我一直在尝试能够使用从相机拍摄的照片或从图书馆中选择的照片,并让它在 map 上放置一个图钉,并将刚刚选择/拍摄的图像作为图钉。

我能够成功调出操作表并让用户选择/拍照。我还可以让用户放下别针。这是我用于这两个操作的代码:

//To take a photo
- (IBAction)takePhoto:(id)sender {
    // Opens Action Sheet
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Add a picture to the map" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Take a picture",@"Choose from photos", nil];
    [actionSheet showInView:self.view];

}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [imageView setImage:image];

    [self dismissModalViewControllerAnimated:YES];
}

- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissModalViewControllerAnimated:YES];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    picker2 = [[UIImagePickerController alloc] init];
    picker2.delegate = self;

    if (buttonIndex == 0) {
        [picker2 setSourceType:UIImagePickerControllerSourceTypeCamera];

    } else if (buttonIndex == 1) {
        [picker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    }
    [self presentModalViewController:picker2 animated:YES];

}

//To drop a pin
        [self.view addSubview:mapview];

    MKCoordinateRegion region = {mapview.userLocation.location.coordinate.latitude,mapview.userLocation.location.coordinate.longitude};
    CLLocationCoordinate2D myCoord = {mapview.userLocation.location.coordinate.latitude,mapview.userLocation.location.coordinate.longitude};
    [mapview setCenterCoordinate:myCoord animated:YES];

    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;
    [mapview setRegion:region animated:YES];
    [mapview setDelegate:self];

    Annotation *ann = [[Annotation alloc]initWithLocation:CLLocationCoordinate2DMake(37.616815,-122.389682)];
    [mapview addAnnotation:ann];
    ann.title = @"Start";
    ann.subtitle = @"Subtitle for annotation";
    ann.coordinate = myCoord;
    ann.image = imageView;

    [self setTitle:@"Map View"];

    [mapview selectAnnotation:ann animated:YES];
    [mapview addAnnotation:ann];

我不确定如何将这些集成在一起并制作它,以便将拍摄的图像用作图钉,非常感谢任何帮助。

最佳答案

添加注释时,可以将图像设置为(使用苹果示例混合的代码):

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[Annotation class]])
    {
        return [self registerAnnotation:annotation withIdentifier:@"AnnotationID" withImage:self.personImage.image];
    }
    else
    {
        return nil;
    }
}

- (MKAnnotationView*)registerAnnotation:(id<MKAnnotation>)annotation withIdentifier:(NSString*)identifier withImage:(UIImage*)image
{
    NSLog(@"annotationIdentifier: %@", identifier);
    MKAnnotationView *flagAnnotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (flagAnnotationView == nil)
    {
        flagAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        flagAnnotationView = [self resizeAnnotationFromAnnotation:flagAnnotationView andSetImage:image];
    }
    else
    {
        flagAnnotationView.annotation = annotation;
    }
    return flagAnnotationView;
}

- (MKAnnotationView*)resizeAnnotationFromAnnotation:(MKAnnotationView*)annotationView andSetImage:(UIImage*)flagImage
{
    annotationView.canShowCallout = YES;
    annotationView.opaque = NO;

    CGRect resizeRect = [self resizeAnnotationFromSize:flagImage.size];
    UIImage *resizedImage = [self resizeImageFromCGRect:resizeRect andImage:flagImage];
    annotationView.image = resizedImage;

    // offset the flag annotation so that the flag pole rests on the map coordinate
    annotationView.centerOffset = CGPointMake(annotationView.centerOffset.x + annotationView.image.size.width/2, annotationView.centerOffset.y - annotationView.image.size.height/2);
    return annotationView;
}

- (UIImage*)resizeImageFromCGRect:(CGRect)resizeRect andImage:(UIImage*)flagImage
{
    UIGraphicsBeginImageContext(resizeRect.size);
    [flagImage drawInRect:resizeRect];
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resizedImage;
}

- (CGRect)resizeAnnotationFromSize:(CGSize)size
{
    // size the flag down to the appropriate size
    CGRect resizeRect;
    resizeRect.size = size;
    CGSize maxSize = CGRectInset(self.view.bounds, [self annotationPadding], [self annotationPadding]).size;
    maxSize.height -= self.navigationController.navigationBar.frame.size.height + [self calloutHeight];

    if (resizeRect.size.width > maxSize.width)
        resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);
    if (resizeRect.size.height > maxSize.height)
        resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);

    resizeRect.origin = CGPointMake(0.0, 0.0);

    return resizeRect;
}

关于ios - 拍照并将其用作 map View 上的图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21913765/

相关文章:

ios - Swift:无法访问声明库中的方法

ios - ARkit 和 Appthinning.plist 错误

ios - 在 iOS 中获取街道名称

swift3 - Xcode 8 - 快速 3 : Creating an image format with an unknown type is an error

ios - 动态创建和定位不同尺寸的 UILabels

ios - 获取 iPhone 所在的国家(没有 CLLocationManager)

ios - 如何使用swift2向 map View 中的第二个注释/pin添加不同的按钮操作

ios - 使用多个图钉在 map 中定位用户的位置

objective-c - 在后台呈现一个 View Controller

ios - UIImagePickerController 中的前置摄像头