1

I'm working on an app that gives the user the option to take a photo for their profile picture, but can't seem to figure out how to:

  1. Get the photo to save to the users library
  2. Get that photo to replace a default photo when they press "use" (that is there when the user first loads the app)

Any suggestions? This code might be completely off but here is what I was starting to use:

- (void)takePhoto {
     UIImagePickerController *takePhotoPicker = [[UIImagePickerController alloc] init];
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        takePhotoPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        takePhotoPicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    } else {
        takePhotoPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    }

    [self presentViewController:takePhotoPicker animated:YES completion:nil];

}
4

1 回答 1

1

您需要做的是将您的 viewController 注册为 UIImagePickerControllerDelegate,然后执行以下操作:

takePhotoPicker.delegate = self;

然后,您需要添加方法:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {}

您可以使用它来获取图像。

要从相机或相册中获取图像,您需要从info字典中的正确键中获取值。

例如,要获取编辑后的图像(由用户调整大小):

UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];

并获得原始图像:

UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];

原始图像是用户使用相机拍摄的全屏图像。

然后您可以使用此图像设置图像视图或上传到服务器等。

此外,您不应该根据相机是否可用来设置来源类型,而应该让用户选择(如果他们想从相册中选择,即使他们有相机)。

于 2013-07-29T19:21:55.423 回答