UIImageView rejects `image` after user chooses it from their library - iOS - ios

I've got some basic code to let the user pick an image and set it to an already created UIImageView:
- (void)addAvatarImageView {
self.avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 83, 83)];
self.avatarImageView.imageURL = [[NSURL alloc] initWithString:self.user[#"avatar"]];
[self.avatarView addSubview:self.avatarImageView];
}
and the picker creation:
- (void)showAvatarPicker {
UIImagePickerController *imagePickController = [[UIImagePickerController alloc] init];
imagePickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickController.delegate = self;
imagePickController.allowsEditing = YES;
[self.navigationController presentViewController:imagePickController animated:YES completion:nil];
}
and the picker delegate:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
[self.avatarImageView setImage:image];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
I can breakpoint inside the delegate method and it goes in there fine, but the UIImageView does not show the new image at all. I can't even remove the image that is present either. Any ideas?

I am going to go on a limb here and say that the error is in the portion of the code that you are not showing: where you instantiate your picker. More presicely, you may not have set allowsEditing to yes. So take a look below
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
Alternatively you can just read the original image using UIImagePickerControllerOriginalImage instead of your current UIImagePickerControllerEditedImage.

change UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; to UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage];

Related

How to overlay camera capture image?

I am replacing the part of image by capture image from camera. i need to perform the activity like this app in my application. https://itunes.apple.com/in/app/replace-my-face-funny-girls/id794033892?mt=8
I need a way of how can i take picture from background view with super view image and save the new generated image with merging of both image.
Please suggest me solution.
The simple work i did
- (void)viewDidLoad {
[super viewDidLoad];
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.delegate = self;
self.picker.allowsEditing = NO;
self.picker.showsCameraControls = NO;
self.picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
CGRect overlayRect = CGRectMake(0, 0, self.imageView.frame.size.width,self.imageView.frame.size.height);
UIView *overlayView = [[UIView alloc] initWithFrame:overlayRect];
UIImageView *capture_image=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.imageView.frame.size.width,self.imageView.frame.size.height)];
capture_image.image=[UIImage imageNamed:#"1.jpg"];
[overlayView addSubview:capture_image];
[self.picker setCameraOverlayView:overlayView];
[self.navigationController presentViewController:self.picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Please also suggest me if there is any repository working like this approach.
About capturing the image: you might want to look at FastttCamera, which allows you to set the camera view to your defined image view and crops the image accordingly.
Good luck!

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

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...

How can I retrieve the image taken using UIImagePickerControllerDelegate with ARC enabled?

I have an app that needs to take a picture and then show it to the user. I opened the camera to let the user take a picture using the following code:
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = #[(NSString *) kUTTypeImage];
imagePicker.allowsEditing = NO;
imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:imagePicker animated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: #"Camera failed to open"
message: #"Camera is not available"
delegate: nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
This present the camera view so the user can take a picture. I implemented the methods to handle the cancel and complete events like this:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:nil];
UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
[photoView setImage:image];
[photoView setNeedsDisplay];
}
The problem is that after going through the last function, image is nil. At first dismissViewControllerAnimated was at the end of the function, but in this SO post the answer explains that dismissViewControllerAnimated should be called first and picker should be released before getting the image from info. I tried putting dismissViewControllerAnimated at the top, but because I'm using ARC I cannot release picker.
How can I get the image from info?
You have set picker.allowsEditing = NO; in your code. Then why are you retrieving the edited image? It won't exist. Anyways, if you allow editing, use this:
photoView.image = info[UIImagePickerControllerEditedImage] ? info[UIImagePickerControllerEditedImage] : info[UIImagePickerControllerOriginalImage]
If the user has edited the photo, photoView.image will be set to the edited photo, otherwise it will be set to the original photo.
If you don't allow editing, simply retrieve the image using the UIImagePickerControllerOriginalImage key.
try this:
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// or UIImage *image = [info objectForKey:#"UIImagePickerControllerEditedImage"];
Check in this method,
(void) imagePickerController:(UIImagePickerController *)_picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"dictionary image info: %#",info );
}
In this method, print the dictionary and check that are you getting UIImagePickerControllerMediaType and UIImagePickerControllerOriginalImage.
(In my case, I m getting value like this UIImagePickerControllerMediaType = "public.image";
UIImagePickerControllerOriginalImage = UIImage: 0x1dd79520";)
Now if you are getting value for UIImagePickerControllerOriginalImage then you save value in this UIImage.
Now use this.
UIImage *image = [info objectForKey:#"UIImagePickerControllerEditedImage"];
OR
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
Hope this will help you.

UIPickerController crashes the app

I have a UIPickerController that gets your pictures and allows you to pick some of them though at the moment when I click the button to activate it the app crashes.
Here is the code that I am using for it:
in my ViewDidLoad method:
pickerController = [[UIImagePickerController alloc] init];
pickerController.allowsEditing = NO;
pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
The function:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:nil];
patientPicture = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
UIImageView *pictureView = (UIImageView *)[imageCell viewWithTag:777];
pictureView.image = patientPicture;
[_imgViewAdd reloadInputViews];
}
And it being called:
- (IBAction)addPicture:(id)sender {
[self presentViewController:pickerController animated:YES completion:nil];
}
It is wierd because I have recently changed my app to Ipad only though while it was in IPhone it worked fine. When you click the button in NSLog this error message crops up which I supose is something to do with it:
UIStatusBarStyleBlackTranslucent is not available on this device.
I suspect this is quite a common issue that people have
Thanks in advance
Try presenting in a popover...
pickerController = [[UIImagePickerController alloc] init];
UIPopoverController *popOverController = [[UIPopoverController alloc] initWithContentViewController:pickerController];
popOverController.delegate = self;
and to present...
[popOverController presentPopoverFromRect:yourframe inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

Resources