Taking a photo and saving it to the Camera Roll - ios

I'm having a problem where I just want a simple solution to take a picture and then save it in my app. However so far all I can do is take the photo have it load in a UIImageView, however it does not save in the Camera Roll.
- (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];
}
- (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];
}
Any help regarding this would be greatly appreciated. Believe me, I really need it.

You will want to do this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
UIImageWriteToSavedPhotosAlbum(chosenImage, nil, nil, nil);
[picker dismissViewControllerAnimated:YES completion:NULL];
}

Swift 4
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if(info[UIImagePickerControllerMediaType] as! String == "public.image"){
guard let curImage:UIImage = info[UIImagePickerControllerOriginalImage] as? UIImage else{
return
}
UIImageWriteToSavedPhotosAlbum(curImage, nil, nil, nil)
}
}

Related

getting null when printing an image in didFinishPickingMediaWithInfo method

I am trying to pick an image from photo library and for that I used picker controller, but when I am trying to print the image in didFinishPickingMediaWithInfo method I am getting a null response. I used protocols also in my .h file.
- (IBAction)choosePhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
NSLog(#"choose the img");
[self presentViewController:picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.imageView.image = info[UIImagePickerControllerEditedImage];
NSLog(#"imgae is %#",self.imageView.image);
[picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
// here you can check that image is null or not
[Btn_ImagePicker setImage:chosenImage forState:UIControlStateNormal];
[picker dismissViewControllerAnimated:YES completion:NULL];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
For the full detail kindly print NSLog(#"%#",info); info contains full details.

UIImagePickerController causing crash

I'm using ios 8.4. When debugging an app, and I present a UIImagePickerController, xcode loses connection with the iphone. This wasn't a problem before.
Sometimes it will bring up the image picker... but then when I save an image, there'll be a crash.
Is anyone else experiencing this? How to fix?
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.editing = NO;
imagePickerController.delegate = self;
imagePickerController.showsCameraControls=YES;
[self presentViewController:imagePickerController animated:YES completion:nil];
try this code
- (IBAction)galleryButtonPressed:(id)sender
{
UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
pickerController.allowsEditing = YES;
pickerController.delegate = self;
[self presentViewController:pickerController animated:YES completion:NULL];
}
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:NULL];
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
if (image == nil)
image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Do something with the image
[self.imageView setImage:image];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:NO completion:nil];
}

Assigning image from photo album to a variable Objective-c

I am getting image from a photo album with this code:
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];}
Please Could anyone help me to assign this image to a variable? I want to blur it later using stackblur:
UIImage *newIma=[oldIma stackBlur:radius];
Thanks in advance!
Set the UIImage when you're getting the image that the user has selected.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
// Create the UIImage in your #implementation and set it here
myImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
myImageView.image = myImage;
}

ImagePickerController for iOS

I am trying to select a picture from the device's photo library.
This is my code so far. I am following a tutorial and that should work, but I am getting an error :
"Use of undeclared identifier info", on the second method, line 3.
What am I doing wrong?
- (IBAction)selectPicturePressed:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo
{
UIImage *chosenImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
self.imgToUpload.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Change info to editInfo will resolve it.

How to load a UIImage from PhotoStream with UIImagePicker and Photo Framework under iOS 8.1?

How do I use the new Photo Framework to get a UIImage from a given image URL which the UIImagePicker returns?
I hope I understood you right...
You can open an UIImagePickerController like this:
- (IBAction)selectPhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
And after picking you get the UIImage from the pickers delegate like this
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
The chosenImage will be your Image
EDITED after complain
Im sorry dude... the Key I have chosen is only available if you set picker.allowsEditing = true; property on true...
So if you don't need the editing after you have chosen your image, use this one
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
instead of
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
I have just tried it, it will work...

Resources