I have implemented picture taking while pressing UI Button butwhen ever i pressed the button got app crashed.
Here is the source code.
.h file
#interface Camera : UIViewController
<UIImagePickerControllerDelegate>
{
UIImagePickerController *mPicture;
}
#property (nonatomic, retain) UIImagePickerController *mPicture;
.m file
#implementaion Camera
#synthesize mPicture;
-(void)pictureButtonPushed
{
UIImagePickerControllerSourceType mType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable:mType])
{
mPicture.sourceType = mType;
[self presentModalViewController:mPicture animated:YES];
}
}
Thanks in advance
Try this one
Hope it will help :)
-(IBAction)takePhoto
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
}
// image picker needs a delegate,
[imagePickerController setDelegate:self];
// Place image picker on the screen
[self presentModalViewController:imagePickerController animated:YES];
}
-(IBAction)chooseFromLibrary
{
UIImagePickerController *imagePickerController= [[UIImagePickerController alloc]init];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
// image picker needs a delegate so we can respond to its messages
[imagePickerController setDelegate:self];
// Place image picker on the screen
[self presentModalViewController:imagePickerController animated:YES];
}
//delegate methode will be called after picking photo either from camera or library
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissModalViewControllerAnimated:YES];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[myImageView setImage:image]; // "myImageView" name of any UImageView.
}
Here is the code for what you want
- (void)cameraPressed
{
UIActionSheet *menu = [[UIActionSheet alloc]
initWithTitle:#"Set a Prifile Picture"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Choose From Library",#"Take a New Photo",nil];
[menu showInView:[self.navigationController view] ];
}
// actionsheet delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
#try {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.navigationBar.barStyle = UIBarStyleBlack;
imagePickerController.delegate = self;
imagePickerController.allowsEditing = NO;
[appDelegate.objList setHidden:TRUE];
appDelegate.strRefreshCamera = #"notupdate";
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];
}
else {
[appDelegate showAlertWithTitle:#"Info" message:#"This function needs a camera which is only available on the iPhone or iPod."];
}
}
#catch (NSException *e) {
}
}
if (buttonIndex == 0) {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.navigationBar.barStyle = UIBarStyleBlack;
imagePickerController.delegate = self;
imagePickerController.allowsEditing = NO;
[appDelegate.objList setHidden:TRUE];
appDelegate.strRefreshCamera = #"notupdate";
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];
}
}
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//flag = TRUE;
[self dismissModalViewControllerAnimated:YES];
//[appDelegate showLoadingView];
UIImage *capturedImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
Use this image object where ever you want to use.
Related
I am trying to display a UIImagePicker from a programatically generated UIView that's added as a subview of the original view controller. The image picker comes up and so does the camera but the functionality breaks and nothing navigates properly. How do I properly load an image picker from a UIView and not a UIControlView?
code that breaks:
- (void)captureImage:(id)sender
{
[[[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:#"Close"
destructiveButtonTitle:nil
otherButtonTitles:#"Take photo", #"Camera Roll", nil]
showInView:self];
}
//UIImagePicker specific methods
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex)
{
case 0:
[self takePhoto];
break;
case 1:
[self fromCameraRoll];
break;
}
}
-(void)takePhoto
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES)
{
// Create image picker controller
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
// Delegate is self
imagePicker.delegate = self;
// Show image picker
[self addSubview:imagePicker.view];
}
}
-(void)fromCameraRoll
{
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self addSubview:imgPicker.view];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//do something with the image
//add code to pick images here and store them in the preview
[picker dismissModalViewControllerAnimated:NO];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:NO];
}
UIImagePickerController is view controller class, you better present your image picker to your parent view controller by a delegate call or something like with an overlay subview to achieve your goal.
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
CustomOverlayView *overlayview = [[CustomOverlayView alloc] init];
imgPicker.cameraOverlayView = overlayview;
[self presentModelViewController:imgPicker animated:YES];
add another delegate in your overlay class to dismiss your camera picker controller view
[imgPicker dismissModelViewControllerAnimated:YES];
I'm trying to present an UIImagePickerController. It works fine on simulator, but after few times of presenting, I get an error in the console.
More than maximum 5 filtered album lists trying to register.
This will fail.
Here is the code:
- (IBAction)imageSelectorPressed:(id)sender
{
UIImagePickerController* picker = [[[UIImagePickerController alloc] init] autorelease];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
// Set source to the Photo Library
picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
}
picker.delegate = self;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
self.imagePopOverController = popover;
[self.imagePopOverController setDelegate:self];
[self.imagePopOverController presentPopoverFromRect:[kepekBtn frame] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[popover release];
}
-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
[popoverController dismissPopoverAnimated:YES];
[self setImagePopOverController:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self.imagePopOverController dismissPopoverAnimated:NO];
[image setImage:[info objectForKey:#"UIImagePickerControllerOriginalImage"]];
picker = nil;
[self setImagePopOverController:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self.imagePopOverController dismissPopoverAnimated:NO];
picker = nil;
[self setImagePopOverController:nil];
}
UPDATE: I've checked the contact app on the iPad. Edited a person, and it has a same error in the console log. BUT it is not crashing, while my app is. So the proper question: How can I fix it?
I implement UIImagePickerController delegate, UINavigationController delegate, UIPopOverController delegate. I have no clue what the problem is. My device restarts after calling this 3, 4 or 5 times ( It is different each build ). Please help me fix it!!
EDIT: I get this error:
More than maximum 5 filtered album lists trying to register. This will fail.
Here is the code I am using to call UIImagePickerController and get the image:
- (IBAction)imgPickerPressed:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
imgPickerTypeActionSheet = [[UIActionSheet alloc] initWithTitle:#"Choose image source:" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Camera", #"Photo Library", nil];
[imgPickerTypeActionSheet showInView:self];
}
else {
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
[controller setDelegate:self];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
[controller setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
imgPickerPopOver = [[UIPopoverController alloc] initWithContentViewController:controller];
imgPickerPopOver.delegate = self;
[imgPickerPopOver presentPopoverFromRect:CGRectMake(imgPickerButton.frame.origin.x, imgPickerButton.frame.origin.x-250, 0.0, 0.0)
inView:self
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if ([actionSheet isEqual:imgPickerTypeActionSheet]) {
if (buttonIndex == 0) {
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
[controller setDelegate:self];
[controller setSourceType:UIImagePickerControllerSourceTypeCamera];
[[delegate getVC] presentModalViewController:controller animated:YES];
}
if (buttonIndex == 1) {
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
[controller setDelegate:self];
[controller setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
imgPickerPopOver = [[UIPopoverController alloc] initWithContentViewController:controller];
imgPickerPopOver.delegate = self;
[imgPickerPopOver presentPopoverFromRect:CGRectMake(imgPickerButton.frame.origin.x, imgPickerButton.frame.origin.x-250, 1, 1)
inView:self
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
}
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[imgPickerPopOver dismissPopoverAnimated:YES];
pickedImageView.image = image;
[self valueChanged:nil];
}
Does this work in the iOS simulator? Also are you releasing any of these objects, or is ARC doing it? This may help.
How can we display the UiImagepicker controller interface with both camera and video mode and also with Photo Library icon button,same as default camera App for iPhone.
or How to remove cancel Button (shown in Camera view) and replace with different button. Is it possible and if will apple approve this approach.
Please help me out ??
You can try this way.
BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
if(hasCamera){
UIActionSheet *actionSheet;
actionSheet = [[[UIActionSheet alloc] initWithTitle:#"Add Photo"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Select from Library", #"Take a New Photo", nil] autorelease];
actionSheet.actionSheetStyle = UIBarStyleBlackOpaque;
[actionSheet showInView:[self view]];
}
else {
UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];
}
Actionsheet delegate method
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
if(buttonIndex == 0)
{
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
else if(buttonIndex == 1)
{
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];
}
Image picker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
if(image)
{
[self.addPhotoButton setBackgroundImage:image forState:UIControlStateNormal];
}
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
}
i have the problem with the select the image from UIImagePickerController in ipad in ios 5.
same code was run perfectly in ios 4 .
so what is the problem ?
can any one help me how to use UIImagePickerController in ios 5 ?
Is there any difference between ios 4 and ios 5 for UIImagePickerController ?
Below is the code for the issue.
-(IBAction)selectExitingPicture:(id)sender
{
if ([popoverController isPopoverVisible]) {
[popoverController dismissPopoverAnimated:YES];
[popoverController release];
} else {
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
popoverController = [[UIPopoverController alloc]
initWithContentViewController:imagePicker];
popoverController.delegate = self;
[popoverController presentPopoverFromRect:CGRectMake(280, 700, 320, 400) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
[imagePicker release];
}
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[popoverController dismissPopoverAnimated:true];
NSString *mediaType = [info
objectForKey:UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:YES];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
selectedImageView.image = image;
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
// Code here to support video if enabled
}
}
After selecting the image from UIImagePicker in the imagePickerController:didFinishPickingMediaWithInfo method i received the image reference 0*0
Thanks
try this
-(IBAction)buttonpressed{
UIImagePickerController *anImagePickerController = [UIImagePickerController new];
anImagePickerController.delegate = self;
anImagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
UIImageView *anImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"overlay_320x427.png"]];
anImageView.frame = CGRectMake(0, 1, 320, 427);
anImageView.hidden = YES;
anImagePickerController.cameraOverlayView = anImageView;
[self presentModalViewController:anImagePickerController animated:YES];
[anImagePickerController release];
[NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:#selector(timerFireMethod:)
userInfo:anImageView
repeats:NO];
[anImageView release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)info {
/* Do something here*/
[self dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissModalViewControllerAnimated:YES];
}