Showing posts with label iPhone. Show all posts
Showing posts with label iPhone. Show all posts

Friday, September 10, 2010

"UIImagePickerController does not work"!


Here is another common mistake that I have seen with UIImagePickerController. Say, I want to display the picked image in the UIImageView (imageView), but it doesn't work. It is a simple issue, can anybody guess what the problem is before you see the answer below?
- (IBAction)takePhoto {

     UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
     imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
     [self presentModalViewController:imagePicker animated:YES];
     [imagePicker release];
}

- (IBAction)choosePhoto {

     UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
     //imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
     [self presentModalViewController:imagePicker animated:YES];
     [imagePicker release];
}

- (void)imagePickerController:(UIImagePickerController *)imagePicker didFinishPickingMediaWithInfo:(NSDictionary *)info {

     UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
     imageView.image = image;

     [self dismissModalViewControllerAnimated:YES];


ANSWER:
We have not set the delegate! So no call backs being obtained! Just add : 
   
 imagePicker.delegate = self;


UIImagePickerController strange behavior

Here is a piece of UIImagePickerController source code. The source code is really straight forward. Say, we have a ViewController and in the .m file, we declare the following information:LoadCamera is a function that open the initiates the UIImagePickerController and adds it to the delegate's window. What's wrong with the code? 

-(IBAction)loadCamera:(UIButton *)sender{
     
     PagingScrollViewAppDelegate *appDelegate = (PagingScrollViewAppDelegate *)[[UIApplication sharedApplication] delegate];

     imagePickerController = [[UIImagePickerController alloc] init];
     imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
     
     imagePickerController.delegate = self;
     
     [appDelegate.window addSubview:imagePickerController.view];
 }
Symptoms:
The strange thing occurs when I select an image inside the UIImagePickerController, instead of calling the
- (void)imagePickerController:(UIImagePickerController *)picker 
          didFinishPickingImage:(UIImage *)image
                      editingInfo:(NSDictionary *)editingInfo
it calls the

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
Answer:
Basically, it is due to the fact that we are adding the UIImagePickerController's view as a subview of the main view.
UIImagePickerController should be presented modally. Else, it gives strange behavior! 

Hope that helps!

UIViewController *viewController = [[[UIViewController alloc] initWithNibName:nil bundle:nil] autorelease];
self.navController = [[[UINavigationController alloc]           
                           initWithRootViewController:viewController] autorelease ];
[appDelegate.window addSubview: [navController view]];
[navController presentModalViewController:myImagePicker animated: YES];

Thursday, September 9, 2010

NSLog tricks: Log only in Debug mode. Add Function name, Line num

It is a common requirement to log certain statements only in the Debug mode, while needing to avoid them in the release mode. Of course, one option is to have statements like this everywhere:

#ifdef DEBUGGING
NSLog(@"Data: point.x = %f, point.y = %f", point.x, point.y);
#endif
And define DEBUGGING somewhere in the header file. However, this can get very verbose. 
A better option is to use Macros. In project_Prefix.pch file, include: 

#ifdef DEBUGGING
# define DBLog(fmt,...) NSLog(@"%@",[NSString stringWithFormat:(fmt), ##__VA_ARGS__]);
#else
# define DBLog(...)
#endif 
Also, you can include, in "Edit Active Target" -> "Build" -> (Select Debug Config) -> "Other C/C++ Flags", write:

-DDEBUGGING 

This automatically defines DEBUGGING flag in Debug mode. Leave it blank for the Release mode.  

Now, you can use DBLog instead of NSLog: 

DBLog(@"Data: point.x = %f, point.y = %f", point.x, point.y);

This will automatically print statements in the Debug mode, and not print the statements in the Release mode! 

(Alternatively, you can have a line in the Prefix.pch file:
#define DEBUGGING 
And manually comment it out for Release mode.)

Similarly, if you want you also have function name and line number, there is a nice trick:

#ifdef DEBUGGING#   define DLog(fmt, ...) NSLog((@"Func: %s , Line: %d, " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#   define DLog(...)
#endif

If you always want to print out regardless of the DEBUGGING SETTING, but with the function name and line number:
#define ALog(fmt, ...) NSLog((@"Func: %s , Line: %d, " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
Hope that helps! Happy iCoding!

Sunday, November 22, 2009

Where to start ?!

So how to go about doing this? Creating your own apps, that is. It can be overwhelming to begin with. So much to learn, and so many different resources. Whether to spend time reading all the documentation, or just jump to all the tutorials and do them one by one? And the verbose Objective-C. It's all confusing.

Having played around with the SDK for the past couple of months, my experience has been that just going through the tutorials will help you make simple apps, however if you want to go farther than that, then you have to spend some time understanding the SDK. Of course, you cannot master the whole material. So the best way is to do the early tutorials to get a feel for what it is like to make an app; then decide what kind of app you would like to make, and then understand those materials in more detail.

So why yet another blog on iPhone programming? One of the things that is still missing from the blogs out there is elaboration on some of the common questions a beginner faces: "what is the difference between these two ways of achieving this result", "why are are there different kinds of templates", so on and so forth. Sometimes, I was left with more questions than answers. I had to dig deeper and get answers to some of those.I would definitely like to elaborate on those issues as I learn more.

So in the next few postings, I will start out with a basic app, and also address some of the basic questions regarding that. We will also understand some of the objective-C programming paradigm.

iCheers :-)

Saturday, November 21, 2009

necessity meets interest..

Greetings!

I just started working at an Eye research institute, where I work on developing computer vision algorithms for cell phone applications. The project is intended to help visually challenged / blind people.

We are targeting the iPhone for our project, which basically brings me to this blog. I am an Apple fan without doubt, and iPhone programming was something I wanted to try since its launch. I am lucky that I am getting to work on something which really excites me as part of my job ... So as I start learning iPhone programming, I thought it would be nice to share my experiences and thoughts. Hope you all find it useful.

Happy iProgramming!