Monday, 26 August 2013

How to pass image from one View to Previous view from Gallery in iPhone?

How to pass image from one View to Previous view from Gallery in iPhone?

I was successfully fetched an image from gallery and It was appearing in
second screen.But when I click on done button this image is send to
previous screen.
Below is detailed explanation
1)First screen initially one image.
2)When I click on this image it navigate into second screen and it ask
choose from gallery
3)I was choose from gallery one image and this image is appearing in
second screen.
4)When I was click on done button in second screen it will navigate into
previous screen that is first screen and the image will be change with
selected image
How to pass my image into previous screen and set with the selected image.
MprofileViewController.h
#import <UIKit/UIKit.h>
#import "AddProfileViewController.h"
@class MProfileViewController;
@interface MProfileViewController :
UIViewController<UIImagePickerControllerDelegate,UITableViewDataSource,UITableViewDelegate,ImageSelectionDelegate>
{
NSMutableArray* titles;
IBOutlet UITableView *mainTableView;
IBOutlet UIImageView *image2;
}
@property(strong,nonatomic)IBOutlet UIImageView *image2;
@property(nonatomic, retain) NSMutableArray *titles;
@property(strong,nonatomic)UITableView *mainTableView;
-(IBAction) clickEventOnImage:(id) sender;
@end
MprofileViewController.m
#import "MProfileViewController.h"
@interface MProfileViewController ()
@end
@implementation MProfileViewController
@synthesize titles,mainTableView;
@synthesize image2;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
titles=[[NSMutableArray alloc]init];
self.navigationItem.title = @"View Profile";
image2.image=[UIImage imageNamed:@"hariku-indah.jpg"];
}
- (void) imageSelected:(UIImage *)image {
// Use image
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction) clickEventOnImage:(id) sender{
AddProfileViewController *Avc = [[AddProfileViewController
alloc]initWithNibName:@"AddProfileViewController" bundle:nil];
Avc.delegate=self;
[self.navigationController pushViewController:Avc animated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO];
}
@end
AddProfileViewController.h
#import <UIKit/UIKit.h>
@protocol ImageSelectionDelegate <NSObject>
- (void) imageSelected:(UIImage*)image;
@end
@interface AddProfileViewController :
UIViewController<UIImagePickerControllerDelegate,
UINavigationControllerDelegate, UIActionSheetDelegate>{
IBOutlet UIImageView *imageView;
NSData *dataImage;
}
// Delegate property
@property (nonatomic,assign) id<ImageSelectionDelegate> delegate;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
-(IBAction)back:(id)sender;
-(IBAction)done:(id)sender;
@end
AddProfileViewController.m
#import "MProfileViewController.h"
@interface AddProfileViewController ()
@property(strong,nonatomic) UIImagePickerController *imagePicker;
@end
@implementation AddProfileViewController
@synthesize imageView;
#pragma mark -
#pragma mark View lifecycle
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"actionSheet");
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0) {
[self pushTakePhotoScreenInDelegate];
}
else if (buttonIndex == 1) {
[self pushChoosePhotoScreenInDelegate];
}
}
-(void)pushTakePhotoScreenInDelegate
{
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage
imageNamed:@"viewfinder_2.png"]];
CGSize screenSize = [UIScreen mainScreen].bounds.size;
[imageView setFrame:CGRectMake(0, -52/2.0, screenSize.width,
screenSize.height)];
self.imagePicker.cameraOverlayView = imageView;
self.imagePicker.delegate = self;
[self presentViewController:self.imagePicker animated:YES
completion:nil];
}
-(void)pushChoosePhotoScreenInDelegate
{
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
self.imagePicker.delegate = self;
[self presentViewController:self.imagePicker animated:YES
completion:nil];
}
//-(void)imagePickerController:(UIImagePickerController*)picker
didFinishPickingMediaWithInfo:(NSDictionary*)info
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary
*)editingInfo
{
self.imageView.image = image;
[picker dismissViewControllerAnimated:YES completion:nil];
}
// In case you are using image picker, this delegate is called once image
selection is complete.
//- (void)imagePickerController:(UIImagePickerController *)picker
//didFinishPickingMediaWithInfo:(NSDictionary *)info
//{
//Use either according to your setting, whether you allow image
editing or not.
//self.imageView.image = image;
//UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];
//For edited image
// UIImage *myImage = [info
objectForKey:UIImagePickerControllerOriginalImage];
//if([_delegate respondsToSelector:@selector(imageSelected:)]) {
// [self.delegate imageSelected:myImage];
// }
//}
/*
-(void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage * pickedImage = [info
objectForKey:UIImagePickerControllerOriginalImage];
AddProfileViewController * controller = [AddProfileViewController new];
controller.imageView.image = pickedImage;
// [self.navigationController pushViewController:controller animated:YES];
}*/
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self.navigationController dismissModalViewControllerAnimated:YES];
}
// Override to allow orientations other than the default portrait
orientation.
-
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"Select Image from..."
delegate:self cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil otherButtonTitles:@"Take Photo",
@"Choose from library", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
}
-(IBAction)back:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)done:(id)sender
{
// if([_delegate respondsToSelector:@selector(imageSelected:)]) {
// [self.delegate imageSelected:imageView];
// }
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:YES];
}
@end

No comments:

Post a Comment