[英]Crop Image from Camera in Swift without move to another ViewController
[英]Move from Viewcontroller to another after image take
好的,我是 iPhone 的菜鳥,我假設有一個簡單的問題。 我在那里找到了很多幫助,但不確定我做錯了什么。 我只想用相機拍照,然后在成功捕獲后移動到下一個視圖控制器並將其放置在圖像視圖中。 從這里得到了大部分代碼,所以已經謝謝了,但似乎無法讓它工作,我不明白所有不同類型的錯誤。 我想一定是我殺死了視圖控制器,然后試圖重新實例化它,但我有點迷失了請幫助。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)TakePhoto:(id)sender{
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:NULL];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage:image];
[self dismissViewControllerAnimated:YES completion:NULL];
NSString *submit = @"viewControllerSubmit";
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:submit bundle:nil];
UIViewController *vc = [mainstoryboard instantiateViewControllerWithIdentifier:submit];
[self presentViewController:vc animated:YES completion:nil];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (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.
}
@end
如您所見,我嘗試了一些方法。 我曾嘗試搜索錯誤,但嘗試其他方法無濟於事。 任何幫助都會很棒,提前致謝。
================================================== ================== 好的,我正在發布我的完整代碼。 我可以讓表格從一個轉到下一個,因為我只是錯誤地調用了故事板名稱。 但是,我無法獲得從相機拍攝的圖像以顯示在下一個表格中。 我得到的只是一個沒有任何錯誤的空 ImageView。 我錯過了什么? 請幫助在這方面工作了好幾天並且已經研究了很多。
第一個視圖控制器 .M 文件
#import "ViewController.h"
#import "ViewControllerSubmit.h"
@interface ViewController ()
@end
@implementation ViewController{
UIImage *newImage;
}
- (IBAction)TakePhoto:(id)sender{
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:nil]; //nil from Null
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"ViewControllerSubmit"]){
ViewControllerSubmit *destViewController = segue.destinationViewController;
destViewController.theNewImage = newImage;
}
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
newImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:NULL];
NSString *submit = @"ViewControllerSubmit";
NSString *main = @"Main";
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:main bundle:nil];
UIViewController *vc = [mainstoryboard instantiateViewControllerWithIdentifier:submit];
[self presentViewController:vc animated:YES completion:nil];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (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.
}
@end
第一個視圖控制器 .H 文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
UIImagePickerController *picker;
}
@end
第二個視圖控制器 .M 文件
#import "ViewControllerSubmit.h"
@interface ViewControllerSubmit ()
@property (weak, nonatomic) IBOutlet UIImageView *_image;
@end
@implementation ViewControllerSubmit
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self._image.image = self.theNewImage;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
第二個視圖控制器 .H 文件
#import "ViewController.h"
@interface ViewControllerSubmit : ViewController
@property (strong, nonatomic) UIImage *theNewImage;
@end
再次,沒有錯誤,相機出現,它拍攝照片,當我單擊“使用照片”時,第二個視圖控制器彈出,但圖像視圖不顯示捕獲的圖像。
我錯過了什么? 任何幫助都會很棒,請再次原諒我的無知,我是 iPhone 的新手。
您需要在完成塊中編寫導航代碼...
試試下面的代碼,
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage:image];
[picker dismissViewControllerAnimated:YES completion:^{
NSString *submit = @"viewControllerSubmit";
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:submit bundle:nil];
UIViewController *vc = [mainstoryboard instantiateViewControllerWithIdentifier:submit];
[self presentViewController:vc animated:YES completion:nil];
}];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.