-1

ユーザーにアプリで写真を撮ってもらいたいだけです→トリミング→保存。したがって、UIImagePickerControl の allowEditing プロパティを使用したいと思います。しかし、私はそれを行う方法がわかりません。これは、カメラ用の今までの私のコードです。

-(IBAction)TakePhoto {
    picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    [picker setSourceType:UIImagePickerControllerSourceTypeCamera];
    [self presentViewController:picker animated:YES completion:NULL];

}
4

1 に答える 1

0

これを使用してカメラから写真を選択します

- (IBAction)takePhoto:(UIButton *)sender {

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentViewController:picker animated:YES completion:NULL];   
}

これを使用して、フォト ライブラリから写真を選択します

- (IBAction)selectPhoto:(UIButton *)sender {

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

[self presentViewController:picker animated:YES completion:NULL];

}

次の 2 つのデリゲート メソッドを使用します

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

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;

[picker dismissViewControllerAnimated:YES completion:NULL];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[picker dismissViewControllerAnimated:YES completion:NULL];

}

これを ViewDidLoad メソッドに追加すると、デバイスがカメラをサポートしていない場合にこの警告メッセージが表示されます。

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                    message:@"Device has no camera"
                                                    delegate:nil
                                                    cancelButtonTitle:@"OK"
                                                    otherButtonTitles: nil];

    [myAlertView show];

}

両方のプロトコルを AppViewController.h ファイルに追加します。

@interface APPViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
于 2013-07-19T09:53:42.717 に答える