2014-04-27 69 views
0

好的,我創建了一個應用程序,可以從攝像頭或相機卷拍攝圖片,並且可以在同一個視圖控制器中選擇圖片後顯示圖像。我想要做的是得到的圖片,並在一個新的視圖控制器加載它,這樣我可以有一個新的工具欄,可以增加覆蓋圖像給它如何在新視圖控制器中查看來自攝像頭/攝像頭的圖像

這是我的按鈕代碼

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

感謝

如果妳想要的任何其他代碼,請讓我知道:)

+0

「相同的視圖控制器」,你的意思是'UIImagePickerController'或提供它的(自定義)視圖控制器? –

+0

我的意思是我已經在我的FirstViewController中設置了一個UIImageView,但是我想將它移動到secondViewController –

+0

嗯,我不知道你的代碼是什麼樣的,但是也許你可以向該視圖控制器添加一個'UIImage'屬性。首先你啓動視圖控制器,然後設置圖像屬性,然後顯示它。 –

回答

0

tutorial會綽綽有餘;)

+0

我使用的教程,它做我想要的所有東西,但是,我從相機中選擇一張圖片並按下選擇後,它將圖像加載到與firstview相同的視圖中,並且該視圖中的按鈕仍然在屏幕中在底部(拍照和選擇照片))我希望它加載在一個新的屏幕上,所以我可以添加一個新選項來添加疊加圖像在我剛剛選擇的圖像的頂部 –

+0

我是否正確地理解了你,_after_你拍了一張照片,你想在另一個視圖中顯示該圖片? –

+0

是的,這是正確的,我希望它顯示在一個新的viewcontroller,所以我可以在視圖控制器上添加一個新的工具欄,並在該工具欄我想要的圖像,我可以拖到圖片作爲覆蓋...像很多在那裏你可以把卡通嘴脣/眼鏡等放在人的照片上的應用程序 –

0

SecondViewController.h

@interface secondViewController 

@property (nonatomic, strong) UIImage *image; 

// Other properties here... 

@end 

SecondViewController.m

// You override the setter so that the image you give it gets displayed in the image view 
- (void)setImage:(UIImage *)image 
{ 
    _image = image; 

    self.imageView.image = image; 
} 

FirstViewController.m

- (void)imagePickerController:(UIImagePickerController *)picker 
didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage]; 
    [picker dismissViewControllerAnimated:YES 
           completion:nil]; 

    SecondViewController *secondVC = [[SecondViewController alloc] init]; 
    secondVC.image = chosenImage; 

    [self presentViewController:secondVC 
         animated:YES 
        compeltion:nil]; 
} 

此方法被調用圖像拾取完成時。當圖像選擇器出現時,不要忘記設置self作爲圖像選擇器的代表。

+0

再次更新問題,我添加了所有你的代碼:) –