【问题标题】:Pass image from camera to view from storyboard将图像从相机传递到情节提要中的视图
【发布时间】:2014-01-17 13:36:41
【问题描述】:

我的代码发生了一些奇怪的事情。

场景:打开相机->拍照->将拍摄的图像传递给视图控制器(来自情节提要。

问题是我的目标视图中的 UIimage 变量不可用,无法识别!

我的代码

 postviewController.h

#import <UIKit/UIKit.h>
@interface postAlertViewController : UIViewController
@property (nonatomic, retain) UIImage *cameraImage;
@end

 postviewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
   UIImageView *aImageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width,150 )];
    aImageview.image=cameraImage;
    [self.view addSubview:amageview];
}

拍照并传递给上面的视图控制器

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:NULL];
    ViewController *postView = [self.storyboard  instantiateViewControllerWithIdentifier:@"postview"];
    postView.cameraImage=(UIImage *)[info objectForKey:UIImagePickerControllerEditedImage];
    [self.navigationController pushViewController:postView animated:YES];
}

故事板 ID 设置为“postview”。 我得到的是:

Property 'cameraImage' not found on object of type 'ViewController *'

为什么cameraImage 在目标的 .h 文件中声明,但在源视图中不可用?

在另一种情况下,我需要在视图之间传递字符串,方式与上面相同

[yourViewController setValue:mysc.title forKey:@"sendAlertTitle"];

工作正常。

有什么帮助吗?

谢谢。

【问题讨论】:

  • 您想在其他视图控制器中显示您的图像吗?
  • 我想我已经解释过了。从 View1 按下按钮,打开相机,拍照,将图片传递给 View 2。
  • 在你的情况下 secondViewController.imageview.image = firstimageview.image;

标签: ios objective-c storyboard


【解决方案1】:

试试这个

postAlertViewController *postView = [self.storyboard  instantiateViewControllerWithIdentifier:@"postview"];

并始终使用以大写开头的类和接口名称

【讨论】:

  • :( 我必须包含 postviewController.h 并使用 postviewController *postview 而不是 ViewController。简单!谢谢!我会接受的。
【解决方案2】:

您可以更轻松地使用这个。

- (IBAction)captureImage:(id)sender
{
    if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        UIAlertView *deviceNotFoundAlert = [[UIAlertView alloc] initWithTitle:@"No Device" message:@"Camera is not available"
                                                                     delegate:nil
                                                            cancelButtonTitle:@"Okay"
                                                            otherButtonTitles:nil];
        [deviceNotFoundAlert show];

    } else {

        UIImagePickerController *cameraPicker = [[UIImagePickerController alloc] init];
        cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        cameraPicker.delegate =self;
        // Show image picker
        [self presentViewController:cameraPicker animated:YES completion:nil];
    }
}



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

    UIImage *selectedImage =[info objectForKey:UIImagePickerControllerOriginalImage];

    ViewControllerName *Vc=[self.storyboard instantiateViewControllerWithIdentifier:@"captioEdit"];

    Vc.postImgStr=[self scaleAndRotateImage:selectedImage];

    [picker dismissViewControllerAnimated:YES completion:nil];

    [self.navigationController pushViewController:captioViewEdit animated:YES];
}

//Delegate method of UIImagePickerController for image picker model cancel
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];

}

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    相关资源
    最近更新 更多