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;
}
#property (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
Related
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
make a variable in your app delegate and synthesize it like 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 your view controller in your 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 the selected image stored in myImage Variable and now you can use it anywhere you want
In your AddprileViewController.h file create UIimage object as
UIImage *_gImage;
and set property as
#property (strong, nonatomic) UIImage *_gImage;
in .m file synthesize this object
#synthesize _gImage;
set image in _gImage object
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
self.imageView.image = image;
//add image in globle object
_gImage = image;
[picker dismissViewControllerAnimated:YES completion:nil];
}
in your MprofileViewController.m
- (void)viewWillAppear:(BOOL)animated {
if(!Avc) {
Avc = [[AddProfileViewController alloc]initWithNibName:#"AddProfileViewController" bundle:nil];
}
if (Avc._gImage != nil) {
image2.image=Avc._gImage;
}
else {
image2.image=[UIImage imageNamed:#"hariku-indah.jpg"];
}
}
Hope this will solve your problem.
set this as global object.
Avc = [[AddProfileViewController alloc]initWithNibName:#"AddProfileViewController" bundle:nil];
How to change my profile picture image, when I click on done button in second screen the first screen image should change with the second screen image.
I am selecting image from Gallery that image was appearing in my second screen how to set this image in first screen view controller while I am clicking on done button.
This is 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
AddprileViewController.m
#import "AddProfileViewController.h"
#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
// if([_delegate respondsToSelector:#selector(imageSelected:)]) {
// [self.delegate imageSelected:imageView]; }
This code is technically correct. But you forgot to synthesize your delegate in AddprofileViewController
I'm trying to pass an image from one view to another. I have a camera button and when press fires off the uiimagepickercontroller. Once the image is selected the edit photo screen is pushed onto the screen. Whenever the screen loads nothing appears. I tried following this post Passing image from one view to another but it seems not to work.
Here is my code for the first view controller:
firstViewController.h
#interface firstViewController : UITableViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate>
- (IBAction)cameraButtonPressed:(id)sender;
firstViewController.m
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:^{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
ECDEditViewController *viewController = [[ECDEditViewController alloc] initWithImage:image];
UINavigationController *composeController = [self.storyboard instantiateViewControllerWithIdentifier:#"showCompose"];
//[self.navigationController presentViewController:composeController animated:YES completion:nil];
[self.navigationController pushViewController:composeController animated:YES];
}];
}
secondViewController.h
#interface ECDEditViewController : UITableViewController <UIImagePickerControllerDelegate>
- (id)initWithImage:(UIImage *)aImage;
#property (strong, nonatomic) IBOutlet UIImageView *myImage;
#property (nonatomic, strong) UIImage *image;
secondViewController.m
#implementation ECDEditViewController
#synthesize myImage;
#synthesize image;
- (id)initWithImage:(UIImage *)aImage {
self = [super init];
if (self) {
if (!aImage) {
NSLog(#"sorry no picture loaded®");
return nil;
}
self.image = aImage;
[myImage setImage:aImage];
NSLog(#"picture loaded");
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"im trying");
[myImage setImage:image];
[myImage setContentMode:UIViewContentModeScaleAspectFit];
}
You need to do something like this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:^{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
ECDEditViewController *composeController = [self.storyboard instantiateViewControllerWithIdentifier:#"showCompose"];
composeController.image = image;
[self.navigationController pushViewController:composeController animated:YES];
}];
}
Your composeController is actually an ECDEditViewController, not a UINavigationController. So this line is unnecessary (as is the initWithImage: method):
ECDEditViewController *viewController = [[ECDEditViewController alloc] initWithImage:image];
On my iPhone app, I have two viewControllers (firstViewController, secondViewController), on firstViewController the user can select a photo from the camera roll and it then displays it in an imageView, however I need to also display it in secondViewController but have no idea how to.
Can you also please explain your answers in-depth as I am fairly new to objective-C
Here's my code:
firstViewController.h
#import <UIKit/UIkit.h>
#interface firstViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
UIImageView *theImageView;
}
#property (nonatomic, retain) IBOutlet UIImageView *theImageView;
-(IBAction)selectExistingPicture;
#end
firstViewController.m
#import "firstViewController.h"
#import "secondViewController.h"
#implementation firstViewController
#synthesize theImageView
-(IBAction) selectExistingPicture
{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage : (UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
theImageView.image = image;
[picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
}
-(IBAction)switchSecondViewController {
SecondViewController *viewcontroller = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self presentModalViewController:viewcontroller animated:YES];
[viewcontroller release];
}
// Default Apple code
#end
There's not much in secondViewController so I won't bother posting that code.
Have a method in your second view controller of the type
-(void)setImage:(UIImage *)image
In the method below after you create a viewcontroller call the method with the image as shown
-(IBAction)switchSecondViewController {
SecondViewController *viewcontroller = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self presentModalViewController:viewcontroller animated:YES];
[viewcontroller setImage:theImageView.image]
[viewcontroller release];
}
You need to declare a property in second view so you can set the image from your first view some thing like this:
secondImageView is the property in secondView and you have to set it
-(IBAction)switchSecondViewController {
SecondViewController *viewcontroller = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
viewcontroller.secondImageView.image = firstViewImage;
[self presentModalViewController:viewcontroller animated:YES];
[viewcontroller release];
}
I have an app where user can take picture from camera and image is displayed in imageview.
It works fine for the first time but the second time app is crashed leaving memory warning level=1
I google around and tried all possible ways but no use
Below is the code i used in my app
UIImagePickerController *imgpicker;
#property(nonatomic,retain) UIImagePickerController *imgpicker;
#synthesize imgpicker;
if(actionSheet.tag==1)
{
NSLog(#"button index:%i",buttonIndex);
if(buttonIndex == 0)
{ NSLog(#"button index for camera:%i",buttonIndex);
if(self.imgpicker==nil){
self.imgpicker=[[UIImagePickerController alloc]init];
self.imgpicker.delegate =self;
self.imgpicker.sourceType=UIImagePickerControllerSourceTypeCamera;
}
[self presentModalViewController:self.imgpicker animated:YES];
[imgpicker release];
return;
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(#"didFinishPickingMediaWithInfo editing info:%#",info);
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
imageview = [[UIImageView alloc] initWithFrame:CGRectMake(65, 10, 75, 75)];
NSData *newjpg = UIImageJPEGRepresentation(image, 0.5);
[imageview setImage:[UIImage imageWithData:newjpg]];
[scrollView addSubview:imageview];
[self.view addSubview:scrollView];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
Please help me
Try changing this:
self.imgpicker=[[UIImagePickerController alloc]init];
to this:
self.imgpicker=[[[UIImagePickerController alloc]init] autorelease];
and remove this line:
[imgpicker release];
If you're showing the image picker more than once, you're leaking memory from the image view that gets allocated every time the picker finishes.
You're also adding the image view to the scroll view and the scroll view to the view controller's view every time. You just need to do it once, and the update the image property on the image view.
Try this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(#"didFinishPickingMediaWithInfo editing info:%#",info);
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
if (imageview == nil) {
imageview = [[UIImageView alloc] initWithFrame:CGRectMake(65, 10, 75, 75)];
[scrollView addSubview:imageview];
[self.view addSubview:scrollView];
}
NSData *newjpg = UIImageJPEGRepresentation(image, 0.5);
[imageview setImage:[UIImage imageWithData:newjpg]];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
Please create UIImagePickerController object in your AppDelegate class and initialize in your AppDidFinishloading method : didFinishLaunchingWithOptions. Realese it on appDelegate dealloc method.
You can access it in your needed view controller Page from appdelegate class.
Please do not create evertytime UIImagePickerController object when u needed and realese it.
This might help you.
Please use this code :
App Delegate
// .h
#import <UIKit/UIKit.h>
#interface MyProjectAppDelegate : NSObject <UIApplicationDelegate> {
UIImagePickerController *image_picker;
UIWindow *window;
UINavigationController *navigationController;
}
#property(nonatomic, retain) UIImagePickerController *image_picker;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
-(void)initializeImagePickerView;
#end
// .m
#import "MyProjectAppDelegate.h"
#import "RootViewController.h"
#implementation MyProjectAppDelegate
#synthesize window;
#synthesize navigationController,image_picker;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self initializeImagePickerView];
// Set the navigation controller as the window's root view controller and display.
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
-(void)initializeImagePickerView
{
// Picker View
self.image_picker = [[UIImagePickerController alloc] init];
self.image_picker.allowsImageEditing = YES;
}
- (void)dealloc {
[image_picker release];
[navigationController release];
[window release];
[super dealloc];
}
#end
//RootView Controller
// .h
#import <UIKit/UIKit.h>
#import "MyProjectAppDelegate.h"
#interface RootViewController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
MyProjectAppDelegate *appDelegate;
}
-(IBAction)btnPhotoPressed:(id)sender;
#end
// .m
#import "RootViewController.h"
#implementation RootViewController
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (MyProjectAppDelegate *)[[UIApplication sharedApplication] delegate];
}
-(IBAction)btnPhotoPressed:(id)sender
{
appDelegate.image_picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
appDelegate.image_picker.delegate=self;
[self presentModalViewController:appDelegate.image_picker animated:YES];
}
#pragma mark UIImagePickerController delegate
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *imgCapturePhoto = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// You can use image here
[self dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
}
- (void)dealloc {
[super dealloc];
}
This will definitely help u.