如何在新的视图控制器中查看来自相机胶卷/相机的图像

时间:2014-04-27 18:14:58

标签: ios imageview viewcontroller

好的,所以我创建了一个从相机或相机胶卷拍照的应用程序,我可以在同一个视图控制器中选择后显示图像。我想要做的是获取图片并将其加载到一个新的视图控制器中,这样我就可以有一个新的工具栏,可以添加叠加图像

这是我的按钮代码

- (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;

}
}

这是我的SecondViewController.h

@interface SecondViewController : UIViewController
<UIImagePickerControllerDelegate,
UINavigationControllerDelegate>

@property BOOL newMedia;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
- (void)useCamera:(id)sender;
- (void)useCameraRoll:(id)sender;
@end

好了还有一些问题,我已经提出了建议的代码,但我的模拟器中没有新的事情发生在我的整个代码我想知道是否有人可以帮助我     //     // BESPOKEViewController.m     //货架规划师     //     //由AppyWorld于2014年4月26日创建。     //版权所有(c)2014 AppyWorld。版权所有。     //

#import "BESPOKEViewController.h"
#import "SecondViewController.h"

@interface BESPOKEViewController ()

@end

@implementation BESPOKEViewController





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

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}// The method that gets called when you want to present the second view controller

- (void)showSecondViewControllerButton:(id)sender
{
UIImage *imageToDisplay = self.imageView.image;

SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.image = imageToDisplay;

// Or present it another way
[self presentViewController:secondViewController animated:YES completion:nil];
}


- (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;

  }
}

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






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])
  {
    // Code here to support video if enabled
  }
  [self dismissViewControllerAnimated:YES completion:nil];}

-(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];
}
}
@end

这是我的SVC.m

 //
 //  Created by AppyWorld on 26/04/2014.
//  Copyright (c) 2014 AppyWorld. All rights reserved.
//

#import "SecondViewController.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.
}
// You override the setter so that the image you give it gets displayed in the image         

view
- (void)setImage:(UIImage *)image
{
_image = image;



}- (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

提前致谢

如果您想要任何其他代码,请告诉我们:)

1 个答案:

答案 0 :(得分:0)

tutorial绰绰有余;)