Issue with code sdk 3.1.3

Posted on 2010-04-03
Medium Priority
Last Modified: 2013-11-25
Hi all,

I am having a bit of an issue with some code.
I basically have 2 actions

using the UIImagePickerController.

Action 1 is:

imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

The above code opens the image library

Action 2 is:

imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;

The above code opens the camera

The code works fine BUT I cannot get both the work, each on a different action assigned to a button.

Here is the whole code below:

#import "PickerViewController.h"

@implementation PickerViewController

@synthesize imgPicker;

- (void)viewDidLoad {
      self.imgPicker = [[UIImagePickerController alloc] init];
      self.imgPicker.allowsImageEditing = YES;
      self.imgPicker.delegate = self;            

- (IBAction)grabImage {
      imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
      [self presentModalViewController:self.imgPicker animated:YES];


- (IBAction)takeImage {
      imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
      [self presentModalViewController:self.imgPicker animated:YES];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
      image.image = img;      
      [[picker parentViewController] dismissModalViewControllerAnimated:YES];
      // need to show the upload image button now
      upload.hidden = NO;



Question by:ST3VO
  • 4
  • 3
LVL 33

Expert Comment

ID: 29631382
I've not tried this scenario, but it should work (in case you have enough memory):
"Prior to running the picker interface, set this value to the desired source type. The specified source type must be available and an exception is thrown if it is not. If you change this property while the picker is visible, the picker interface changes to match the new value in this property."

Here are two nice tutorials

In the last link, in the comment after the article, you will see the code for the camera and the switch from the camera to the photo gallery.

Here is a nice tutorial or FAQ:
UIImagePickerController, UIImage, Memory and More!


Author Comment

ID: 29642246
Hi and thanks from your reply. Thinks are good but it's not a memory issue. I have 2 actions and whichever I run first retains. For example if I click on grabimage first the 2 buttons retain the sourcetype and vica versa, so I need to sort that out.
LVL 33

Accepted Solution

pgnatyuk earned 2000 total points
ID: 29644950
Try to switch to UIImagePickerControllerSourceTypeSavedPhotosAlbum.

For a test purpose (or, maybe, it will good for you), I'd try to re-create the UIImagePickerController - release and alloc again.

Actually the code taking an image is here:

if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[[self navigationController] presentModalViewController:picker animated:YES];
[picker release];

If you will read this: http://chris-software.com/index.php/2009/04/29/loading-images/

Here is an interesting thread:

P.S. About the memory issue:
Memory usage in UIImagePickerController
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

LVL 33

Expert Comment

ID: 29645222
Seems like here is a simple app that does exactly what you need:
Picking Images with the iPhone SDK UIImagePickerController

Author Comment

ID: 29647495
Hmmm...looks promising thx. Let me check it out and get back to you when I get home. Thx a lot!

Author Comment

ID: 29688765
Downloaded the dmg file and extracted the code and opened it and tried to run it but it give me errors so I cannot check out that download provided on the link. Will continue looking at the code and see If I can figuare out a fix. Will look at the links you provided.

Author Closing Comment

ID: 31710623
Thanks :o)

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question