Captured image is not getting saved in the device - ios

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];
}
}

Related

<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;
});
}

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 push viewcontroller bug with allowsEditing

I am trying to push a view controller (for extra editing) from UIImagePickerController after user has selected an image.
It is successfully pushed.
But when that view controller is popped and user return to the editing screen, the editing screen is no longer interactable. Does anyone know what is the problem.
-(void)didTapBtn:(id)sender
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.picker.delegate = self;
self.picker.allowsEditing = YES;
[self presentViewController:self.picker animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
if (image == nil) {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
if (image == nil) {
return;
}
self.test = [[BTTestViewController alloc] init];
self.test.delegate = self;
[self.picker pushViewController:self.test animated:YES];
}
//Called by BTTestViewController
-(void)didCancel
{
[self.picker popViewControllerAnimated:YES];
}
try this one it work in my application:
UIImagePickerController *imagePicker;//put it in .h file
put it in .m file
imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"STEP-BY-STEP-STORY!" message: #"Camera is not available" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *pickedImage = [info objectForKey:UIImagePickerControllerEditedImage];
imageData.image =[self resizeImage:imageData.image width:200 height:200];
[picker dismissViewControllerAnimated:YES completion:nil];
iphone_filtersViewController *nav=[[iphone_filtersViewController alloc]initWithNibName:#"iphone_filtersViewController" bundle:nil];
[self.navigationController pushViewController:nav animated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
// release Picker
[picker dismissViewControllerAnimated:YES completion:nil];
}
// iphone_filtersViewController method
-(IBAction)backButton
{
[self.navigationController popViewControllerAnimated:YES];
}
may be it will help.

How to use the allowsEditing

I just want the user to take a photo in app→crop→save. So I would want to use the allowsEditing property of UIImagePickerControl. However I don't know how i can do it. this is my code until now for camera.
-(IBAction)TakePhoto {
picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:NULL];
}
Use this for pick photo from camera
- (IBAction)takePhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
use this for pick photo from photo library
- (IBAction)selectPhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
use following two delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Add this in your ViewDidLoad method, so if your device is not support camera then display this alert message.
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Device has no camera"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[myAlertView show];
}
Add both protocols to the AppViewController.h file:
#interface APPViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

Resources