UIImagePickercontroller not saving without use button - ios

I'm using UIImagePickerController to take picuture and save to photo gallery. When i launch the picker it has the button to photo take and cancel after taking photo it shows 2 button Retake & use, if i use, use button then only image saving to photo album but after saving i can't go to previous page or close the picker.
-(void)takepicture:(id)sender{
// 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 presentModalViewController:imagePicker animated:YES];
// [self performSelector:#selector(onTimer_Loadpicture:) withObject:nil afterDelay:0.5];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// Save image
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
// UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[picker release];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissModalViewControllerAnimated:YES];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;
// Unable to save the image
if (error)
alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
else // All is well
alert = [[UIAlertView alloc] initWithTitle:#"Success"
message:#"Image saved to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
[self performSelector:#selector(onTimer_Loadpicture:) withObject:nil afterDelay:0.5];
}

just dismiss your view controller. like this ,update your didFinishPickingMediaWithInfo method
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// Save image
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
// UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[picker dismissViewControllerAnimated:YES completion:NULL];
}

You are not dismissing your view controller.You should do like this and if part of this code is not necessary it is up to you that you want to check your image in imageView or not.If you do not want to check then simply remove the if part and use else part's code.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editingInfo{
[self dismissModalViewControllerAnimated:YES];
_imgview.image = [editingInfo valueForKey:#"UIImagePickerControllerOriginalImage"];
if(_imgview==nil){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Angry!!!" message:#"Vennligst velg et bilde!" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}
else{
_imgview.image = img;
imageData=[self compressImage:img];
[picker dismissModalViewControllerAnimated:YES];
_lblname.hidden=true;
}
return;
}

Related

Captured image is not getting saved in the device

I have created two options in the screen.
To capture the image from camera using : UIImagePickerControllerSourceTypeCamera
To load the image from the UIImagePickerControllerSourceTypeSavedPhotosAlbum.
But when i am capturing the image, image is not storing in the device?
My code is:
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(#"From didDismissWithButtonIndex - Selected Option: %#", [actionSheet buttonTitleAtIndex:buttonIndex]);
NSString*image=[actionSheet buttonTitleAtIndex:buttonIndex];
if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:#"Camera"]) {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:#"Device Camera Is Not Working" delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];
return;
}
else{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}
else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:#"Gallery"])
{
UIImagePickerController *pickerView = [[UIImagePickerController alloc] init];
pickerView.allowsEditing = YES;
pickerView.delegate = self;
[pickerView setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
//[pickerView setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:pickerView animated:YES completion:nil];
}
}
#pragma mark - PickerDelegates
//=====================================Image picker ===============================================
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage* orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:isRowIndex inSection:isSectionIndex] ;
UITableViewCell *cell = [jobstable cellForRowAtIndexPath:indexPath];
UIImageView *tableIMAGE=(UIImageView *)[cell.contentView viewWithTag:19];
tableIMAGE.image=orginalImage;
imageStris = [UIImageJPEGRepresentation(tableIMAGE.image,1)base64Encoding];
answersARRAY[indexPath.row] = [NSString stringWithFormat:#"-1,%#,%#",answersARRAY[indexPath.row],imageStris];
[self visubull];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Just put this line in - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ to store image
When you capture image you have to store it manually.
UIImageWriteToSavedPhotosAlbum(orginalImage,nil, nil, nil);
Hope this will help you
It's because you're not doing anything with the image. You assign it as b64 string to an array and that's it. You must save it to the camera roll manually:
UIImageWriteToSavedPhotosAlbum(tableIMAGE.image, nil, nil, nil);
You have to save image camera roll in didFinishPickingMediaWithInfo like this
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
if(picker.sourceType==UIImagePickerControllerSourceTypeCamera)
{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest creationRequestForAssetFromImage:image];
} completionHandler:nil];
}
}

Get image location from iOS UIImagePickerController

I am try to develop iOS app which get the location of the Photo using UIImagePickerController and display it I am deploying it on iOS 7 device Here is what i did .
- (IBAction)takePhoto:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
picker.showsCameraControls = YES;
[self presentViewController:picker animated:YES
completion:^ {
// [picker takePicture];
}];}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"Media Info: %#", info);
NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
// Get the location property from the asset
CLLocation *location = [myasset valueForProperty:ALAssetPropertyLocation];
// I found that the easiest way is to send the location to another method
latitude =location.coordinate.latitude; //[[gpsdata valueForKey:#"Latitude"]floatValue];
longitutde =location.coordinate.longitude;
NSString * strLocation=[NSString stringWithFormat:#"La:%f Lo%f",latitude,longitutde];
};
// This block will handle errors:
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(#"Can not get asset - %#",[myerror localizedDescription]);
// Do something to handle the error
};
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);
}
}
[picker dismissModalViewControllerAnimated:YES];
}
- (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];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}
I am not able to get .How to get the location ? please help me on this .
in the delegate implementation you can use the path of the image or the image itself:
// This method is called when an image has been chosen from the library or taken from the camera.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//You can retrieve the actual UIImage
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
//Or you can get the image url from AssetsLibrary
NSURL *path = [info valueForKey:UIImagePickerControllerReferenceURL];
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
Location Services Should be enabled when the photo was taken.Try same code by enabling location services.

How to save photo after clicking in iOS [duplicate]

This question already has an answer here:
Save a photo to the camera roll and make sure it actually saved
(1 answer)
Closed 8 years ago.
Here's my button to initiate the camera:
-(IBAction)TakePhoto{
// check if camera exists...to test on simulator
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
picker=[[UIImagePickerController alloc] init];
picker.delegate =self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:NULL];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"" message: #"Camera not available on this device" delegate: nil cancelButtonTitle:#"Ok" otherButtonTitles:nil]; [alert show];
}
}
I just want it to save it to the photo library and then come back to the view controller.
Got it working like this: Added these 2 delegates, didFinishPickingMediaWithInfo and
imagePickerControllerDidCancel.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
image= [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[imageView setImage:image];
[self dismissViewControllerAnimated:YES completion:NULL];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
Just do:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[picker dismissViewControllerAnimated:YES completion:nil];
}

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.

How can I dismiss the image picker after didFinishSavingWithError?

All I want to do is after the alert is shown that the picture was or wasn't saved the picker should be dismissed, but I can't manage to find out how.
I present the image control like this :
[self presentModalViewController:imagePicker animated:YES];
I know i need to dismissmodalviewcontroller, because i do it for the cancel case, but I don't know where to place it so that after the image is saves it exists.
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;
// Unable to save the image
if (error)
alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
else // All is well
alert = [[UIAlertView alloc] initWithTitle:#"Success"
message:#"Image saved to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
You will have to create the instance of UIImagePickerController locally and in alertview delegate method, simply dismiss it.
it is depends on how you present imagepickercontroller if you have used peserntmodelviewcontrolleranimated: then use dismissModalViewControllerAnimated:

Resources