Tuesday, May 22, 2012

Can’t add UIImagePickerViewController’s view as a sub view – iOS 4

Incorrect code sample

UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
pickerController.delegate = self;
[self.navigationController.view addSubview:pickerController];
[pickerController release];

Actually there is nothing wrong with the addSubview method. The only problem is that it doesn’t load the gallery. In iOS 5 the gallery gets loaded without any issue. However if you want to make your app compatible with iOS 4 then you will have to either push the UIImagePickerController or present it as modal. If you don't like the transition animation you can always choose to push/present modal without the animation.

[self.navigationController presentModalViewController:pickerController animated:YES];
OR
[self.navigationController pushViewController:pickerController animated:YES];

No comments: