好的,我創建了一個應用程序,可以從攝像頭或相機卷拍攝圖片,並且可以在同一個視圖控制器中選擇圖片後顯示圖像。我想要做的是得到的圖片,並在一個新的視圖控制器加載它,這樣我可以有一個新的工具欄,可以增加覆蓋圖像給它如何在新視圖控制器中查看來自攝像頭/攝像頭的圖像
這是我的按鈕代碼
- (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 // Shelf Planner // // Created by AppyWorld on 26/04/2014。 //版權所有(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
感謝
如果妳想要的任何其他代碼,請讓我知道:)
「相同的視圖控制器」,你的意思是'UIImagePickerController'或提供它的(自定義)視圖控制器? –
我的意思是我已經在我的FirstViewController中設置了一個UIImageView,但是我想將它移動到secondViewController –
嗯,我不知道你的代碼是什麼樣的,但是也許你可以向該視圖控制器添加一個'UIImage'屬性。首先你啓動視圖控制器,然後設置圖像屬性,然後顯示它。 –