UIImagePickerController 和 UINavigationController

Posted

技术标签:

【中文标题】UIImagePickerController 和 UINavigationController【英文标题】:UIImagePickerController and UINavigationController 【发布时间】:2010-11-19 21:19:03 【问题描述】:

好吧,我有 2 个视图控制器,拳头有三个按钮,每个按钮代表图像数组中的一个图像。当用户按下按钮时,第二个视图控制器上的移动只有一个 IBOutlet UIImageView 来查看图像。我如何使用 UIImagePickerController 来完成这个编程技巧?这是我的文件:

//  PhotoListViewController.h


#import <UIKit/UIKit.h>
#import "PhotoDetailViewController.h"



@interface PhotoListViewController : UIViewController 

    IBOutlet UIButton *button;
    IBOutlet UIImageView *image1;
    IBOutlet UIImageView *image2;
    IBOutlet UIImageView *image3;

    IBOutlet UILabel *label1;
    IBOutlet UILabel *label2;
    IBOutlet UILabel *label3;

    IBOutlet UILabel *nameMikro1;
    IBOutlet UILabel *nameMikro2;
    IBOutlet UILabel *nameMikro3;

    NSString *nameMikroProp;

    IBOutlet PhotoDetailViewController *photoDetailViewController;


@property (nonatomic, retain) PhotoDetailViewController *photoDetailViewController;


-(IBAction)showImage:(id)sender;

@property (copy) NSString *nameMikroProp;

@end



//  PhotoListViewController.m


#import "PhotoListViewController.h"
#import "PhotoDetailViewController.h"

@implementation PhotoListViewController
@synthesize nameMikroProp;
@synthesize photoDetailViewController;



-(IBAction)showImage:(id)sender
    photoDetailViewController = [[PhotoDetailViewController alloc]init];
    //photoDetailViewController.delegate = self;

    if([sender tag] == 4)
        //[photoDetailViewController.imageViewprop setImage:[UIImage imageNamed:@"zaab.png"]];
        //[self presentModalViewController:self.imgPicker animated:YES];

    
    else if([sender tag] == 5)
    
    else
    

    [self.navigationController pushViewController:photoDetailViewController animated:YES];
    [photoDetailViewController release];







// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 


    NSMutableArray *imageArray = [[NSMutableArray alloc] init];
    [imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"main" ofType:@"jpg"]]];
    [imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"main2" ofType:@"jpg"]]];
    [imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"main3" ofType:@"jpg"]]];
    NSMutableArray *nameArray = [[NSMutableArray alloc] init];
    [nameArray addObject:@"zaab's photo one"];
    [nameArray addObject:@"zaab's photo two"];
    [nameArray addObject:@"zaab's photo three"];

    nameMikro1.text  = nameMikroProp;
    nameMikro2.text = nameMikroProp;
    nameMikro3.text = nameMikroProp;

    if([@"zaab" isEqualToString:nameMikro1.text])  
        [image1 setImage:[imageArray objectAtIndex:0]];
        [image2 setImage:[imageArray objectAtIndex:1]];
        [image3 setImage:[imageArray objectAtIndex:2]];
        [label1 setText:[nameArray objectAtIndex:0]];
        [label2 setText:[nameArray objectAtIndex:1]];
        [label3 setText:[nameArray objectAtIndex:2]];
    

    else if([@"evza" isEqualToString:nameMikro1.text]) 
        [image1 setImage:[UIImage imageNamed:@"evza.jpg"]];
    

    [imageArray release];
    [nameArray release];


    [super viewDidLoad];






- (void)didReceiveMemoryWarning 
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.


- (void)viewDidUnload 
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;



- (void)dealloc 
    [nameMikro1 release];
    [nameMikro2 release];
    [nameMikro3 release];
    [label1 release];
    [label2 release];
    [label3 release];
    [image1 release];
    [image2 release];
    [image3 release];
    [super dealloc];



@end


//  PhotoDetailViewController.h


#import <UIKit/UIKit.h>


@interface PhotoDetailViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>  

    IBOutlet UIImageView *imageViewprop;
    UIImagePickerController *imgPicker;



@property (nonatomic, retain) UIImageView *imageViewprop;
@property (nonatomic, retain) UIImagePickerController *imgPicker;

- (IBAction)grabImage;


@end


//  PhotoDetailViewController.m


#import "PhotoDetailViewController.h"


@implementation PhotoDetailViewController
@synthesize imageViewprop;
@synthesize imgPicker;

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo 
    imageViewprop.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"main" ofType:@"jpg"]];  
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];


- (IBAction)grabImage 
    [self presentModalViewController:self.imgPicker animated:YES];



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
    [super viewDidLoad];
    self.imgPicker = [[UIImagePickerController alloc] init];
    self.imgPicker.allowsImageEditing = NO;
    self.imgPicker.delegate = self;
    self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;




- (void)didReceiveMemoryWarning 
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.


- (void)viewDidUnload 
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;



- (void)dealloc 
    [super dealloc];



@end

【问题讨论】:

【参考方案1】:

为什么需要图像选择器控制器来显示图像? ImagePickerController 用于从相册或使用相机中选择图像。

您可以做的是将图像从 PhotoListViewController 传递给 PhotoDetailViewController。

在PhotoDetailViewController中,可以有一个UIImage的属性(合成),这样就可以在加载之前进行设置。然后在 PhotoDetailVC 中使用 UIImageView 来显示图像。 如果您正在寻找更高级的图像功能,请查看

处的three20 框架

three20 at github。

【讨论】:

感谢您的回答 karim 但我知道这种方式。我还想使用 imagePickerController 来制作它。如果您知道这样做的方法,请告诉我。提前致谢。

以上是关于UIImagePickerController 和 UINavigationController的主要内容,如果未能解决你的问题,请参考以下文章

CIDetector 和 UIImagePickerController

UIImagePickerController 不是全屏

是否可以设置 ABPeoplePicker 和 UIImagePickerController 的内容插入?

裁剪和旋转 UIImagePickerController 选择的照片

UIImagePickerController 在 iOS 11 上泄漏

UIImagePickerController 和 UINavigationController