I am new to iPhone development. I have two views named pickerviewcontroller and secondviewcontroller which have different xib files. I choose a picture through the UIImagePickerController interface from the Photo Library, and I was trying to display the chosen image in the second view. pickerController is my first view controller.
pickerController.h file
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface pickerControllerViewController : UIViewController<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate> {
IBOutlet UIButton *selectpic;
UIImageView *imageView;
}
@pro开发者_运维百科perty (nonatomic,retain) UIImageView *imageView;
@property (nonatomic,retain) UIButton *selectpic;
-(IBAction)getpic:(id)sender;
//-(void)goNext: (UIImagePickerController *)picker;
@end
pickerController.m fie
#import "pickerControllerViewController.h"
@implementation pickerControllerViewController
@synthesize imageView,selectpic;
-(IBAction)getpic:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
SecondViewController *secview = [[SecondViewController alloc]initWithNibName:nil bundle:nil];
[secview setImage:imageView];
}
SecondVIewController.h file
@interface SecondViewController :UIViewController{
IBOutlet UIImageView *imageView2;
}
-(void)setImage:(UIImage *)image;
@end
SecondVIewController.m file
@implementation SecondViewController
-(void)setImage:(UIImage *)image{
imageView2 = image;
}
It is not showing any error. I am not able to display the image in the secondview.
In firstviewcontroller.m
<#import "pickerExampleViewController.h"
@implementation pickerExampleViewController
@synthesize selectPic;
-(IBAction)getpic:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.editing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
[picker release];
}
#pragma mark imagePickerController delegate methods
-(void)imagePickerController:(UIImagePickerController *) picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
[picker dismissModalViewControllerAnimated:YES];
Second *secview = [[Second alloc] initWithNibName:@"Second" bundle:nil];
secview.view.backgroundColor = [UIColor blackColor];
[secview.imgView setImage:image];
[self.view addSubview:secview.view];
[secview release];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
}<
In second.m you have to write
<#import "Second.h"
@implementation Second
@synthesize imgView;
-(void)setImage:(UIImage *)img
{
[imgView setImage:img];
}
-(IBAction)back
{
[self.view removeFromSuperview];
}
-(void)dealloc
{
[imgView release];
[super dealloc];
}
@end
<
Create A NSData Variable in pickerControlle class and Store The Image Data in NSData
Then Pass The NSData To Next Class
精彩评论