在rootViewController中使用UIImagePickerController,然后将所选图像传递给secondViewController

时间:2014-05-17 18:09:00

标签: ios uiimageview uiimagepickercontroller segue

这是我的第一个应用程序。

我想为一家朋友公司创建这个,他拥有一家木工公司,他问我是否有可能,我100%可以做到这一点,所以我有机会。基本上我希望能够拍摄墙壁的照片,然后从应用程序中预设的货架列表中进行选择,并将货架图像拉伸到所需尺寸(如果需要,可能会添加更多货架),然后保存货架图像在墙上并通过电子邮件发送给我的朋友的企业电子邮件,我希望用户能够在电子邮件中添加一些文字和内容。

我首先发布我的代码,然后解释问题

RootViewController.h,它的名字叫SpViewController.h

#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "secondViewController.h"
@interface spViewController : UIViewController


<UIImagePickerControllerDelegate,
UINavigationControllerDelegate>

@property BOOL newMedia;


@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) spViewController *secondViewController;
@property (strong, nonatomic) UIImageView *image;
@property (strong, nonatomic) UIImage *myImage;

- (IBAction)useCamera:(id)sender;


- (IBAction)useCameraRoll:(id)sender;


@end

spViewController.m

   #import "spViewController.h"
#import "secondViewController.h"

@interface spViewController ()

@end

@implementation spViewController

- (IBAction)backtohome:(UIStoryboardSegue *)unwindSegue
{
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void) viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)useCamera:(id)sender {
        if ([UIImagePickerController isSourceTypeAvailable:
             UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *imagePicker =
            [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.sourceType =
            UIImagePickerControllerSourceTypeCamera;
            imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
            imagePicker.allowsEditing = NO;
            [self presentViewController:imagePicker
                               animated:YES completion:nil];
            _newMedia = YES;
        }
    }

- (IBAction)useCameraRoll:(id)sender {

    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType =
        UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
        imagePicker.allowsEditing = NO;
        [self presentViewController:imagePicker
                           animated:YES completion:nil];
        _newMedia = NO;
    }
}



//image picker delegate

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = info[UIImagePickerControllerMediaType];

    [self performSelector:@selector(myMethod:) withObject:info afterDelay:0.1];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = info[UIImagePickerControllerOriginalImage];

        _imageView.image = image;
        if (_newMedia)
            UIImageWriteToSavedPhotosAlbum(image,
                                           self,
                                           @selector(image:finishedSavingWithError:contextInfo:),
                                           nil);
    }
    else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
    {

    }
}

-(void)myMethod:(NSDictionary *)info {
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"Perform segue");
        [self performSegueWithIdentifier:@"Picture Unwind Segue" sender:self];
    }];
}


-(void)image:(UIImage *)image
finishedSavingWithError:(NSError *)error
 contextInfo:(void *)contextInfo
{
    if (error) {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle: @"Save failed"
                              message: @"Failed to save image"
                              delegate: nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
    }
}
//cancel delegate

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

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier]isEqualToString:@"Picture Unwind Segue"]) {
        secondViewController *destinationViewController = [segue destinationViewController];
        destinationViewController.myImage = self.myImage;
    }
}

@end

secondViewController.h

#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "spViewController.h"

@interface secondViewController : UIViewController

<UIImagePickerControllerDelegate,
UINavigationControllerDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) UIImage *myImage;
@property (strong, nonatomic) UIImageView *image;

@end

secondViewController.m

#import "secondViewController.h"
#import "spViewController.h"

@interface secondViewController ()

@end

@implementation secondViewController



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

我使用的是故事板,基本上我想要做的就是在我的UIImagepicker运行之后然后选择了一张图片,如果可能的话加载“secondViewController”,然后显示在该视图控制器中选择的图像,在我的第二个VC上的故事板我有一个ImageView和一个工具栏,此刻我没有使用工具栏,我已经尝试了各种不同的代码,但没有一个工作,所以我已经把它剥离回裸基础,所以任何人,实际上知道我需要做什么不需要告诉我删除什么,我知道它不是强制性的,但我认为id喜欢用segue做这个。

我已经开始研究这几天了,但是我知道这需要时间,我真的很感谢你的帮助:)谢谢你提前

编辑:有一个新问题,现在第二个视图控制器加载,我无法在secondviewcontroller上的uiimageview中显示所选图像

1 个答案:

答案 0 :(得分:0)

我认为你应该看看放松细分。我知道这不准确,但我认为它是为dismissViewControllerAnimated:方法分配标识符。 This是如何使用它们的。

在继续之前,请继续为您的故事板添加一个展开的segue,并将其命名为#34; Picture Unwind Segue&#34;

所以在你的didFinishPickingMediaWithInfo:方法中,添加一个UIImage类型的属性并更改:

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];

[self dismissViewControllerAnimated:YES completion:nil];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    UIImage *image = info[UIImagePickerControllerOriginalImage];

    _imageView.image = image;
    if (_newMedia)
        UIImageWriteToSavedPhotosAlbum(image,
                                       self,
                                           @selector(image:finishedSavingWithError:contextInfo:),
                                       nil);
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{

}
}

到此:

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];

self.myImage = info[UIImagePickerControllerOriginalImage];
[self performSegueWithIdentifier:@"Picture Unwind Segue" sender:nil];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    UIImage *image = info[UIImagePickerControllerOriginalImage];

    _imageView.image = image;
    if (_newMedia)
        UIImageWriteToSavedPhotosAlbum(image,
                                       self,
                                           @selector(image:finishedSavingWithError:contextInfo:),
                                       nil);
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{

}
}

并添加方法:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier]isEqualToString:@"Picture Unwind Segue"]) {
        secondViewController *destinationViewController = [segue destinationViewController];
        destinationViewController.myImage = self.myImage;
    }
}

在这里,您将图像保存到变量,然后当您执行segue时,将调用准备segue方法,以便您可以...准备它。所以你在这里做的就是给目的地视图控制器提供图片,是的。现在你的secondViewController有你的图片。我希望这对你有所帮助。

编辑:对于窗口heiarchy问题,试试这个:

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];

[self performSelector:@selector(myMethod) withObject:info afterDelay:0.0];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    UIImage *image = info[UIImagePickerControllerOriginalImage];

    _imageView.image = image;
    if (_newMedia)
        UIImageWriteToSavedPhotosAlbum(image,
                                       self,
                                           @selector(image:finishedSavingWithError:contextInfo:),
                                       nil);
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{

}
}

-(void)myMethod:(NSDictionary *)info {
    self.myImage = info[UIImagePickerControllerOriginalImage];
    [self performSegueWithIdentifier:@"Picture Unwind Segue" sender:nil];
}