UIImagePickerController Taking Picture From Camera - Default to square - ios

Want to provide only square photo capture option from our app. Right now with the below code its giving square crop option after capturing. But want it to be exactly like default camera square option. Also we don't want to show anything other than square.
Below is the code we are using.
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypeCamera;
controller.allowsEditing = YES;
controller.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
// controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
controller.delegate = self;
[self.navigationController presentViewController: controller animated: YES completion: nil];
Attached the screen shot
This is our app
We want it to be like this.

set allowEditing to YES. from the result dict use the key UIImagePickerControllerEditedImage
then you will have the squared image.
i find no way to let the user select, which format he want :(

as Shubham Narang has mention, use UIImagePickerControllerEditedImage in imagePickerController:didFinishPickingMediaWithInfo method.
UIImage *originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];
if(!originalImage)
{
originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
}
This code will use original image if edited (cropped) image doesn't exist

Related

How can i detect the library image is from front camera or back camera

I know its not a good question to be ask but i am stuck. How can i detect when user pick image from library not from camera and this library image saved via front camera or back camera? Like
if (library image from front camera)
{
// Do something here
}
else {
// Do something here
}
Your code checks for available cameras on the device. What you need to do is read the metadata for the image after you have taken the picture, that will include info on the camera.
Use this solution to read the Exif data that comes with the image to find out which camera obtained it: Exif Data from Image
You can check the image EXIF data in the info dictionary UIImagePicker passes in it's callback.
- (IBAction) handleTakePhoto:(UIButton *)sender {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
__block NSDictionary* metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"%#", [metadata valueForKeyPath:#"{Exif}.LensModel"]);
[picker dismissViewControllerAnimated:YES completion:nil];
});
}
The above snippet outputs
iPhone 6 Plus back camera 4.15mm f/2.2
You would have to parse out the "front" or "back" parts of the string.
Relying on parsing something that is parsed out of a string raises some red flags -- there is probably a better and more stable way of doing it.

uiimagepickercontrolleroriginalimage memory waring

I have a collection view (UICollectionViewController subclass) where I placed a UIBarButtonItem named "Take a photo". When I tap the bar button item my camera opens & I take a photo. In my project after "Use Photo" from camera it will move to a crop view controller where I crop the image after selecting the image from my crop view the image gets placed here in my collection view the current controller. In crop view I have two buttons use and cancel.My problem is when I take oddly 2,3 or 4 I receive a memory warning in console then the app crashes with a alert "application terminated due to memory pressure". I've been using this code, when I use UIImagePickerControllerEditedImage there is no issue instead if I use UIImagePickerControllerOriginalImage I come across memory issue.
My problem is to provide a good quality image and this UIImagePickerControllerOriginalImage provides a good quality than UIImagePickerControllerEditedImage.I have to get rid of this memory issue and so I used this line [self dismissViewControllerAnimated:YES completion:NULL];but I couldn't able clear the memory warning.
- (IBAction)TakeaPhoto:(id)sender {
[[UIApplication sharedApplication]setStatusBarHidden:FALSE withAnimation:NO];
gallery=0;
picker1 = [[UIImagePickerController alloc] init];
picker1.delegate = self;
self.resizeableCropArea =YES;
self.cropSize=CGSizeMake(296, 350);
picker1.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker1 animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:
UIImagePickerControllerOriginalImage];
image_cap = [self imageTemp:image scaledToSize:CGSizeMake(320, 370)];
dataTemp = UIImageJPEGRepresentation(image,0.0);
CropViewController *cropController = [[CropViewController alloc] init];
cropController.sourceImage = [info objectForKey:UIImagePickerControllerOriginalImage];
Original_img = UIImageJPEGRepresentation(cropController.sourceImage,0.0);
[original_image addObject:[UIImage imageWithData:Original_img]];
NSLog(#"source image=%#",cropController.sourceImage);
cropController.resizeableCropArea = self.resizeableCropArea;
cropController.cropSize = self.cropSize;
cropController.delegate = self;
Cancel_Image= cropController.sourceImage;
[self dismissViewControllerAnimated:YES completion:NULL];
[self.navigationController
pushViewController:cropController animated:YES];
}
Maybe you should compress your JPEG images. For example if you need that your images have a size less than 70kb, you can use this code:
float compressionRate = 0.90; // Initial compression rate
float maxCompressionRate = 0.10; // Max compression rate
NSData *data = UIImageJPEGRepresentation(outputImage, compressionRate);
// Our limit of size is MAX_UPLOAD_SIZE (70000) and compressionRate is 0.03f
while ([data length] > MAX_UPLOAD_SIZE && compressionRate >= maxCompressionRate) {
compressionRate -= 0.03;
data = UIImageJPEGRepresentation(outputImage, compressionRate);
}
Note: you can edit MAX_UPLOAD_SIZE with you desired size in bytes.

Capture image with out saving on Device using UIImagePickerController

Hi is it possible to capture an Image with out saving to ios device .This is a question that is worrying me.
Can any please give me an idea how to achieve it.
Yes it is possible:
- (void)takePhoto
{
UIImagePickerController * pc = [[UIImagePickerController alloc] init];
pc.sourceType = UIImagePickerControllerSourceTypeCamera;
pc.delegate = self;
pc.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
pc.allowsEditing = YES;
[self presentViewController:pc animated:YES completion:^{
}];
}
#pragma mark - UIImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{
UIImage * image = [info valueForKey:#"UIImagePickerControllerOriginalImage"];
self.imageView.image = image;
}];
}
Edit:
If you want to save the image you can simply save it to the Caches directory (see the apple docs for NSFileManager for info on how to do this, or other stack overflow questions. This is preferred to NSUserDefaults although that would work too.
If you want to simply send it (via email, share, or API upload) you dont have to save it first. You can use the in-memory version that resides in the self.imageView.image property above.

How do I load an image from the photo roll and add other images from the roll on top of it, possibly sort it, and save the final product?

I'm trying to make some composite images from currently existing ones in the photo rolls.
I have code that seems to work that loads an image from the photo roll onto a UIImageView and displays it to the user; (here's the Action that does that:)
- (IBAction)grabImage:(id)sender
{
if ([_myPopoverController isPopoverVisible])
[_myPopoverController dismissPopoverAnimated:YES];
else
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects: (NSString *) kUTTypeImage, nil];
imagePicker.allowsEditing = NO;
_myPopoverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
_myPopoverController.delegate = self;
[_myPopoverController presentPopoverFromRect: [sender frame] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES ];
_newMedia = NO;
}
}
}
next I want to be able to select more photos, but instead of replacing the current one I want them to be layered one on top of another. Finally, when the user is done layering images, I want them to be able to save the final product to the photo roll.
How do I implement this? Not sure where to start...
thanks in advance,
marina
It turns out that all that was needed to do this is just add CALayers to an UIImageView with various degrees of alpha; instead of wiping out the previous image and replacing it with a new one, I simply add differently alpha's layers of images. Works like a charm!

How to detect UIImagePickerController Preview state?

I use standart image picker to make some camera photo.
When user makes photo image picker shows him the Preview screen with 2 buttons "Retake" and "Use".
How to detect that Preview screen is active now or "Retake" button pressed? Is it possible ? Are the useful properties or events? Something like when image source is library the is property - allows editing, which shows similar screen .
UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
A bit after the fact, but maybe someone is still seeking this answer like I was. If you want continue using the native camera controls, you can check the subviews of the ImagePickerController to determine if the post-record view is showing.
BOOL videoTaken = NO;
for (UIView *aView in self.imagePickerController.view.subviews[0].subviews[0].subviews[0].subviews)
{
if ([aView isKindOfClass:NSClassFromString(#"PLTileContainerView")])
{
videoTaken = YES;
break;
}
}
The "PLTileContainerView" is the subview that contains the editing slider that lets you view your video frame by frame, so if it's present, that means your video has already recorded.
For use:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:NO];
NSString *type = [info objectForKey:#"UIImagePickerControllerMediaType"];
if ([type isEqualToString:#"public.movie"]) {
} else {
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
}
For Cancel you don't have a way of detecting it (other than subclassing UIImagePickerController, which may be prohibited, or other way that I'm not aware), but for sure the second cancel is detectable :
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}

Resources