4

シーンUIImagePickerControllerに送信できるようにカメラ画像を保存するにはどうすればよいですか?CreateViewControllerUIImageView

HomeViewControllerUIImagePickerController(シーン 1) には、カメラからの画像を読み込んで返すボタンがあります。CreateViewController(シーン 2) には空のUIImageView.

HomeViewController.h

#import "CreateViewController.h"

@interface HomeViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (strong, nonatomic) UIImagePickerController *imagePicker;
@property(nonatomic, retain) UIImage *myImage;

- (IBAction)cameraImage:(id)sender;

HomeViewController.m

@implementation HomeViewController
@synthesize imagePicker, myImage;

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ( [segue.identifier isEqualToString:@"create"]) {
    CreateViewController *cvc = [segue destinationViewController];
    UIImage *image = myImage;
    cvc.myImage = image;

}
}

//Camera button action
- (IBAction)cameraImage:(id)sender{
//UIImagePickerController space in memory
imagePicker = [[UIImagePickerController alloc]init];
//Set the delegate
imagePicker.delegate = self;
//Set the sourceType
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
//Show Image Picker UI
[self presentViewController:imagePicker animated:YES completion:^{}];
}

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

self.myImage = [info objectForKey:UIImagePickerControllerOriginalImage];

[self dismissViewControllerAnimated:YES completion:^{}];

[self performSegueWithIdentifier:@"create" sender:self];
}

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

CreateViewController.h

@property (strong, nonatomic) IBOutlet UIImageView *bgImage;
@property(nonatomic, retain) UIImage *myImage;

CreateViewController.m

@implementation CreateViewController
@synthesize bgImage, myImage;

- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = myImage;
[bgImage setImage:image];
}

ここからサンプル コードをダウンロードしてください。http://code-blind.com/ios6-camera-picture-to-another-scenes-uiimageview/

4

4 に答える 4

5

プログラムでコントローラーをインスタンス化する場合、セグエの使用は必須ではありません。

それを達成する最も簡単な方法は次のようなものです

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

    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage * pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    DisplayViewController * controller = [DisplayViewController new];
    controller.imageView.image = pickedImage;
    [self.navigationController pushViewController:controller animated:YES];
}

セグエを使用したい場合は、次のようにして手動で呼び出す必要があります

[self performSegueWithIdentifier:@"your-segue-name" sender:self];

次に、 HomeViewController で ivar を使用して を保存し、pickedImageそれを に渡すことができますdestinationViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    
    DisplayViewController * controller = (DisplayViewController *)segue.destinationViewController;
    controller.imageView.image = _pickedImage;
}

_pickedImage画像が選択された後に画像を保存した ivar はどこにありますか。

于 2012-12-11T00:28:10.427 に答える
1

私たちの最初のビューで

  - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    CreateViewController* sI = [self.storyboard instantiateViewControllerWithIdentifier:@"XXXXX"];
    sI.myImage = image;
}

XXXXXはビューの識別子です。

于 2012-12-12T11:43:07.460 に答える
0

まず、デリゲートメソッドが呼び出され、イメージがあることを確認する必要があります。次に、画像をプロパティに保存します。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {


    self.takenImage = image; // assuming you have declared @property(nonatomic, strong) UIImage *takenImage;
    [picker dismissViewControllerAnimated:YES completion:^{

        [self performSegueWithIdentifier:@"my-segue" sender:self];   
    }];

}

その後、このメソッドが呼び出されます。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // you need to set the picked image to the controller:

    DisplayViewController *controller; // get the controller
    controller.image = self.takenImage;
}
于 2012-12-11T00:10:16.680 に答える