UIImagePickerController и UINavigationController

Ну, у меня есть 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

person Mpampinos Holmens    schedule 19.11.2010    source источник


Ответы (1)


Зачем вам нужен контроллер средства выбора изображений для отображения изображения? ImagePickerController используется для выбора изображения из альбома или с помощью камеры.

Что вы можете сделать, так это передать изображение из PhotoListViewController в PhotoDetailViewController.

В PhotoDetailViewController у вас может быть свойство UIImage (синтезированное), чтобы вы могли установить его перед загрузкой. Затем в PhotoDetailVC используйте UIImageView для отображения изображения. Если вы ищете более продвинутую функциональность с изображением, посмотрите на платформу three20 по адресу

three20 на github.

person karim    schedule 19.11.2010
comment
спасибо за ваш ответ, карим, но я знаю этот путь. Я также хотел бы сделать это с помощью imagePickerController. Если вы знаете способ сделать это, дайте мне знать. Заранее спасибо. - person Mpampinos Holmens; 22.11.2010