XCode UIActionSheet image - ios

I am creating a UIButton and when the user clicks on it, the camera should pop up and allow the user to take a photo. I decided to add a UIActionSheet which allows the user to choose wether they want to choose a photo from the album, or whether they want to take a photo using the camera. Before I added the action sheet the button worked fine, when the user shaped the photo, the background image of the button was replaced by the photo taken by the UIImagePicker. But after I added the action sheet now the UIButtons image is not being replaced by the images taken from the UIImagePicker or from the users photo library
here is the associated code,
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
[self.navigationItem setHidesBackButton:NO];
[self.usernameField becomeFirstResponder];
uploadPhotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
[uploadPhotoButton setImage:[UIImage imageNamed:#"uploadphotoicon"] forState:UIControlStateNormal];
uploadPhotoButton.frame = CGRectMake(0, 0, 80, 80);
uploadPhotoButton.clipsToBounds = YES;
uploadPhotoButton.layer.cornerRadius = 80/2.0f;
uploadPhotoButton.layer.borderColor = [UIColor colorWithRed:.102 green:.737 blue:.612 alpha:1.0].CGColor;
uploadPhotoButton.layer.borderWidth = 2.0f;
[self.view addSubview:uploadPhotoButton];
[uploadPhotoButton setCenter:CGPointMake(160, 128)];
[uploadPhotoButton addTarget:self action:#selector(uploadPhotoButton:) forControlEvents:UIControlEventTouchUpInside];
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex < 2) {
UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
UIImagePickerControllerSourceType type;
switch (buttonIndex) {
case 0:
type = UIImagePickerControllerSourceTypePhotoLibrary;
break;
case 1:
type = UIImagePickerControllerSourceTypeCamera;
break;
default:
break;
}
imagePicker.sourceType = type;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:^{ }];
}
}
- (IBAction)signupDone:(id)sender {
NSString *username = [self.usernameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *password = [self.passwordField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([username length] == 0 || [password length] == 0 /*|| [email length] == 0*/){
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Oops!" message:#"Please enter a username, password, and email!" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alertView show];
}
else {
PFUser *newUser = [PFUser user];
newUser.username = username;
newUser.password = password;
[newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry" message:[error.userInfo objectForKey:#"error"] delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alertView show];
}
else {
[self.navigationController popToRootViewControllerAnimated:YES];
}
}];
}
}
-(IBAction)uploadPhotoButton:(id)sender {
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#"Select Photo" delegate:self cancelButtonTitle:#"Nevermind" destructiveButtonTitle:nil otherButtonTitles:#"From Library", #"From Camera", nil];
[sheet showInView:self.view];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
uploadPhotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
uploadPhotoButton.frame = CGRectMake(0, 0, 80, 80);
uploadPhotoButton.clipsToBounds = YES;
uploadPhotoButton.layer.cornerRadius = 80/2.0f;
uploadPhotoButton.layer.borderColor = [UIColor colorWithRed:.102 green:.737 blue:.612 alpha:1.0].CGColor;
uploadPhotoButton.layer.borderWidth = 2.0f;
[self.view addSubview:uploadPhotoButton];
[uploadPhotoButton setCenter:CGPointMake(160, 128)];
[uploadPhotoButton addTarget:self action:#selector(uploadPhotoButton:) forControlEvents:UIControlEventTouchUpInside];
userImage = [info objectForKey:UIImagePickerControllerEditedImage];
[uploadPhotoButton setImage:userImage forState:UIControlStateNormal];
[self.navigationController dismissViewControllerAnimated:YES completion:NULL];
[self uploadImage];

You do not need to create another button,
Only you need to declare uploadPhotoButton in interface for global use in this controller and just replace the method below :
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:NULL];
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
[uploadPhotoButton setImage:chosenImage forState:UIControlStateNormal];
// [self.navigationController dismissViewControllerAnimated:YES completion:NULL];
[self uploadImage];
}
In this code I first diss miss the image pickerViewController and just set the image on my button.

Related

Xcode 8: [Generic] Creating an image format with an unknown type is an error Objective C

I am getting the above error when using uiimagepicker controller. Here is my code:
- (BOOL) startCameraControllerFromViewController:(UIViewController *)controller andisImageFromCamera:(BOOL)isCameraCapture {
if(self.picker == nil) {
/**< instansiate UIImagePickerController object */
self.picker = [[UIImagePickerController alloc] init];;
}
/**< Check isSourceTypeAvailable for possible sources (camera and photolibrary) */
if (([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera] == NO) || ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO))
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Oops!! This feature is not supported on your device" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alertView show];
return NO;
}
self.picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
self.picker.delegate = self;
self.picker.mediaTypes =[NSArray arrayWithObjects: (NSString *) kUTTypeMovie, (NSString *) kUTTypeImage, nil];
self.picker.videoMaximumDuration = MAX_VIDEO_DURATION;
self.picker.allowsEditing = YES;
self.picker.videoQuality = UIImagePickerControllerQualityTypeMedium;
/**< if editing is required*/
[self presentViewController:self.picker animated:YES completion:^{
}];
return YES;
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSURL *videoURL = [info valueForKey:UIImagePickerControllerMediaURL];
NSString *pathToVideo = [videoURL path];
if(![pathToVideo isEqualToString:#""] && pathToVideo != nil) {
BOOL okToSaveVideo = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(pathToVideo);
if (okToSaveVideo) {
//UISaveVideoAtPathToSavedPhotosAlbum(pathToVideo, self, #selector(video:didFinishSavingWithError:contextInfo:), NULL);
}
}
if ([info[UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeMovie]) {
// video
isVideoFromGallery = YES;
isMediaTypeVideo = YES;
[self videoCaptureSaveAction:videoURL andMediaInfo:info];
}
else{
isMediaTypeVideo = NO;
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.previewImage = chosenImage;
self.capturedImageData = UIImageJPEGRepresentation(chosenImage, 90);
[self performSegueWithIdentifier:#"previewCapture" sender:self];
}
}
Picking the video is working. But when i am selecting the image i am gettin the error: [Generic] Creating an image format with an unknown type is an error. I have read other posts about this error, changed the delegate method, but still its not working. Please help.

<Error>: CGImageCreateWithImageProvider: invalid image size: 150 x 150. error in __connection_block_invoke_2: Connection interrupted

When I am running my app on a device (8.0.2) (not the simulator), there are two errors simultaneously.
One is
<Error>: CGImageCreateWithImageProvider: invalid image size: 150 x 150.
Another is
error in __connection_block_invoke_2: Connection interrupted
Here is the code:
- (IBAction)chooseImage:(id)sender {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
self.actionSheet = [[UIActionSheet alloc] initWithTitle:#"choose picture"
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"take photos", #"from photo album", nil];
}
else
{
self.actionSheet = [[UIActionSheet alloc] initWithTitle:#"choose picture"
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"from photo album", nil];
}
self.actionSheet.tag = 1000;
[self.actionSheet showInView:self.view];
}
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet.tag == 1000)
{
NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
switch (buttonIndex)
{
case 0:
sourceType = UIImagePickerControllerSourceTypeCamera;
break;
case 1:
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
case 2:
return;
}
}
else
{
if (buttonIndex == 2)
{
return;
}
else
{
sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
}
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES;
imagePickerController.sourceType = sourceType;
[self presentViewController:imagePickerController animated:YES completion:^{
}];
}}
#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{}];
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
[self.chooseImage setImage:image forState:UIControlStateNormal];
//self.productImage.image = image;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
It's a bug in iOS 10.
The Method - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info changes to - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
Add GCD this way:
[picker dismissViewControllerAnimated:YES completion:^{}];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
[self.chooseImage setImage:image forState:UIControlStateNormal];
//self.productImage.image = image;
});
}

How to load captured image from photo album

I have a button to capture picture then it save to photoAlbum. I used another button for load image from using imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; then i implemented didFinishPickingMediaWithInfo: for loading photo album to newImage. My need is after capture photo i have alertview then click YES button automatically load last captured image PhotoAlbum and add newImage. Is it possible?
- (void)saveImageToPhotoAlbum
{
[self.library saveImage:[self stillImage] toAlbum:#"Album" withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(#"Big error: %#", [error description]);
}
}];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Gallery"
message:#"Do you want to use the captured image?" delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"Yes", nil];
alertView.tag = 2;
[alertView show];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//set image
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
newImage = [[UIImageView alloc] initWithImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
[newImage setFrame:CGRectMake(0, 0, 320, 568)];
[self.view addSubView:newImage];
}
I need to display last captured image to newImage click of alertView button:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 1){
}
}
Before use below code take UIImage *img in .h file
- (void)saveImageToPhotoAlbum
{
[self.library saveImage:[self stillImage] toAlbum:#"Album" withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(#"Big error: %#", [error description]);
}
}];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Gallery"
message:#"Do you want to use the captured image?" delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"Yes", nil];
alertView.tag = 2;
[alertView show];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//set image
img=nil;
img=[info objectForKey:UIImagePickerControllerOriginalImage];
[self saveImageToPhotoAlbum];
// [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if(newImage)
{
[newImage removeFromSuperview];
newImage=nil;
}
newImage = [[UIImageView alloc] initWithImage:img];
[newImage setFrame:CGRectMake(0, 0, 320, 568)];
[self.view addSubView:newImage];
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
else
{
img=nil;
[picker dismissViewControllerAnimated:YES completion:nil];
}
}

iOS error in camera imagepicker program

I Would create a app with camera but when i write this code in ImageViewController.m
xcode: EXPECTED METODY BODY and "."
how i can fix this?
thanks
-(BOOL) launchCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>)
BOOL truefalse = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; //variable to check whether there is a camera available
//if there is a camera, the delegate passed exists, and the controller passed exists, proceed on, otherwise don't go any further
if (!truefalse || (delegate == nil) || (controller == nil)) {
NSLog(#"no can do, delegate/camera/view controller doesn't exist!");
return NO;
}
UIImagePickerController *cameraController = [[UIImagePickerController alloc] init];
cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
cameraController.allowsEditing = NO;
cameraController.delegate = delegate;
It says you have not method at all. Try changing to this (note the braces after method name and after all code):
-(BOOL) launchCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>) delegate{
BOOL truefalse = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; //variable to check whether there is a camera available
//if there is a camera, the delegate passed exists, and the controller passed exists, proceed on, otherwise don't go any further
if (!truefalse || (delegate == nil) || (controller == nil)) {
NSLog(#"no can do, delegate/camera/view controller doesn't exist!");
return NO;
}
UIImagePickerController *cameraController = [[UIImagePickerController alloc] init];
cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
cameraController.allowsEditing = NO;
cameraController.delegate = delegate;
}
Can You Try this.
- (IBAction)presentPicker:(id)sender {
// **********************************************
// * Show action sheet that will allow image selection from camera or gallery
// **********************************************
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:(id)self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Image from Camera", #"Image from Gallery", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
actionSheet.alpha=0.90;
actionSheet.tag = 1;
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
}
#pragma mark - Image picker methods
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex {
switch (actionSheet.tag) {
case 1:
switch (buttonIndex) {
case 0:
[self showCameraImagePicker];
break;
case 1:
[self showGalleryImagePicker];
break;
}
break;
default:
break;
}
}
- (void)showCameraImagePicker {
#if TARGET_IPHONE_SIMULATOR
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Simulator" message:#"Camera not available." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
#elif TARGET_OS_IPHONE
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
// [(UIViewController *)self.delegate presentModalViewController:picker animated:YES];
#endif
}
- (void)showGalleryImagePicker {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
// [(UIViewController *)self.delegate presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage: (UIImage *)image editingInfo:(NSDictionary *)editingInfo {
// [picker dismissModalViewControllerAnimated:NO];
[picker dismissViewControllerAnimated:YES completion:NULL];
photo.image = image;
//[self presentImageCropperWithImage:image];
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
// [picker dismissModalViewControllerAnimated:NO];
[picker dismissViewControllerAnimated:NO completion:nil];
// Extract image from the picker
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"]){
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
photo.image = image;
[picker dismissViewControllerAnimated:YES completion:NULL];
// [self presentImageCropperWithImage:image];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}

UIImagePickerController - [picker dismissViewControllerAnimated:YES completion:Nil]; Error

I have been trying this for the past 2 days and I am not able to figure out the answer. I have searched all over and I haven't found the answer.
The question is I have a button which brings up the camera in my app(to take photos only). The camera opens up, but when i take a picture and click on "USE"(which is displayed at the bottom right) its crashing. Also, when the camera opens up, before taking a picture when I click "Cancel" it again crashes.
I tried using breakpoints and found out that, When I click on the "USE" button, it crashes in this line
[picker dismissViewControllerAnimated:YES completion:Nil]
I'm testing it in my iPad (iOS6).
Here is the Button Code here :
-(IBAction)getAlbum:(id)sender {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSArray *media = [UIImagePickerController
availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
if ([media containsObject:(NSString*)kUTTypeImage] == YES) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
[picker setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeImage]];
picker.delegate = self;
//[self presentModalViewController:picker animated:YES]; //Since [Modal](http://stackoverflow.com/questions/12445190/dismissmodalviewcontrolleranimated-deprecated) has been removed
[self presentViewController:picker animated:YES completion:Nil];
//[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unsupported!"
message:#"Camera does not support photo capturing."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unavailable!"
message:#"This device does not have a camera."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
imagePickerController Method here:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"Media Info: %#", info);
NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
if([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
UIImage *photoTaken = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//Save Photo to library only if it wasnt already saved i.e. its just been taken
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(photoTaken, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
}
selectedLogoImg.image=photoTaken; //selectedLogoImg is the imageView
[self.clipartItemView addSubview:selectedLogoImg]; // To detect touch and move it I place it as a subview of self.clipartItemView
}
//[picker dismissModalViewControllerAnimated:YES];
[picker dismissViewControllerAnimated:YES completion:Nil]
//[picker release];
//[picker dismissViewControllerAnimated:YES completion:^{
// NSLog(#"Dismiss completed");
//}];
}
didFinishSavingWithError Code Here:
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
UIAlertView *alert;
//NSLog(#"Image:%#", image);
if (error) {
alert = [[UIAlertView alloc] initWithTitle:#"Error!"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
imagePickerControllerDidCancel Code Here:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
//[picker dismissModalViewControllerAnimated:YES];
/*[picker dismissViewControllerAnimated:YES completion:^{
[self.view sendSubviewToBack:cardGalleryView];
}];*/
[picker dismissViewControllerAnimated:YES completion:Nil];
}
You should send the dismissViewControllerAnimated:completion: message to the view controller, not the picker. Try:
[self dismissViewControllerAnimated:YES completion:nil];
The above method is only for iOS 6. You need to use [self dismissModalViewControllerAnimated:YES] for iOS 5 and below.
Take a look at the description of the method in the documentation:
Dismisses the view controller that was presented by the receiver.
The presenting view controller is responsible for dismissing the view
controller it presented. If you call this method on the presented view
controller itself, however, it automatically forwards the message to
the presenting view controller
This problem is due to your UIImagePickerController *picker object . ViewController isn't able to identify your picker object reference out of the getAlbum method scope.
1.> you can create UIImagePickerController object in your .h file
#interface yourViewController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate,UINavigationControllerDelegate,UIPopoverControllerDelegate>
{
UIImagePickerController *picker;
UIPopoverController *popover;
}
and in .m file you just use it inside getAlbum IBAction method
-(IBAction)getAlbum:(id)sender {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSArray *media = [UIImagePickerController
availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
if ([media containsObject:(NSString*)kUTTypeImage] == YES) {
UIButton *btn=(UIButton *)sender;
if ([popover isPopoverVisible])
{
[popover dismissPopoverAnimated:YES];
popover=nil;
}
picker = [[UIImagePickerController alloc]init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[picker setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeImage]];
picker.delegate = self;
popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromRect:CGRectMake(btn.frame.size.width,btn.frame.size.height/2,1,1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unsupported!"
message:#"Camera does not support photo capturing."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unavailable!"
message:#"This device does not have a camera."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)myimage editingInfo:(NSDictionary *)editingInfo
{
myimage = [myimage fixOrientation];
[picker dismissModalViewControllerAnimated:YES];
[popover dismissPopoverAnimated:YES];
}
I hope it helps you to better understand.

Resources