ios - How to pass image from one View to Previous view from Gallery in iPhone? -
i fetched image gallery , appearing in second screen.but when click on done button image send previous screen.
below detailed explanation
1)first screen 1 image.
2)when click on image navigate second screen , ask choose gallery
3)i choose gallery 1 image , image appearing in second screen.
4)when click on done button in second screen navigate previous screen first screen , image change selected image
how pass image previous screen , set 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 resources can 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"); // user clicked 1 of 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 using image picker, delegate called once image selection complete. //- (void)imagepickercontroller:(uiimagepickercontroller *)picker //didfinishpickingmediawithinfo:(nsdictionary *)info //{ //use either according setting, whether 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 allow orientations other default portrait orientation. - (bool)shouldautorotatetointerfaceorientation:(uiinterfaceorientation)interfaceorientation { // return yes 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 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 resources can recreated. } -(void)viewwillappear:(bool)animated{ [self.navigationcontroller setnavigationbarhidden:yes]; } @end
make variable in app delegate , synthesize this
in appdelegate.h
@interface appdelegate : nsobject <uiapplicationdelegate> { uiimage *myimage; } @property (nonatomic, retain) uiimage *myimage;
in appdelegate.m
@implementation appdelegate @synthesize myimage;
then create appdelegate instance in view controller in addprileviewcontroller.m
-(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; appdelegate *appdel=(appdelegate *)[uiapplication sharedapplication].delegate; appdelegate.myimage=pickedimage; }
now selected image stored in myimage variable , can use anywhere want
Comments
Post a Comment