在使用按钮上保存照片单击相机应用程序

时间:2013-01-21 10:59:55

标签: ios xcode camera uiimagepickercontroller

我目前的代码是:

- (IBAction)cameraButtonAction:(id)sender {   
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.allowsEditing = NO;
    picker.sourceType = (sender == cameraButton) ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self presentModalViewController:picker animated:YES]; 
}

这会打开默认相机,但是当我选择use时,它不会保存图片。我试过了:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    [self dismissModalViewControllerAnimated:YES];
}

我看过的每个教程,他们使用UIImageView制作自定义相机应用程序,但这不是我想要做的,我不想在应用程序中显示图像并希望保留默认的相机功能。

1 个答案:

答案 0 :(得分:1)

您的代码是正确的。但是你没有设置代理

您在这里缺少一行:

picker.delegate = self;

您需要在cameraButtonAction方法中添加它。

- (IBAction)cameraButtonAction:(id)sender
{   
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = NO;
    picker.sourceType = (sender == cameraButton) ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self presentModalViewController:picker animated:YES]; 
}