How to crop a UIImagePickerController image as it is displayed

When I take a photo using UIImagePickerController, the image is larger than what was visible on the screen ... How can I take a picture / crop it so that it is exactly the same as I saw on the screen?

edit: I will add code to indicate my problem:

- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType { UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext; imagePickerController.sourceType = sourceType; imagePickerController.delegate = self; imagePickerController.showsCameraControls = NO; imagePickerController.allowsEditing = YES; if (sourceType == UIImagePickerControllerSourceTypeCamera) { [[NSBundle mainBundle] loadNibNamed:@"OverlayView" owner:self options:nil]; self.overlayView.frame = imagePickerController.cameraOverlayView.frame; imagePickerController.cameraOverlayView = self.overlayView; //self.overlayView = nil; self.overlayImage.image = self.imageView.image; self.overlayImage.alpha = 0.5; UISlider *slider=[[UISlider alloc]initWithFrame:CGRectMake(60, 80, 200, 30)]; [slider setMaximumValue:1.0]; [slider setMinimumValue:0.0]; [slider setValue:0.5]; [slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged]; if (self.working == TRUE) { [imagePickerController.cameraOverlayView addSubview:self.overlayImage]; [imagePickerController.cameraOverlayView addSubview:slider]; } } self.imagePickerController = imagePickerController; [self presentViewController:self.imagePickerController animated:YES completion:nil]; } 

didFinishPickingMediaWithInfo:

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [self dismissViewControllerAnimated:YES completion:NULL]; //this works, image is displayed UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; //this doesn't work, image is nil UIImage *image = [info objecyForKey:UIImagePickerControllerEditedImage]; [self.imageView setImage:image]; self.working = TRUE; self.imagePickerController = nil; //[self dismissViewControllerAnimated:YES completion:NULL]; } 

Is it possible to use UIImagePickerControllerEditedImage when imagePickerController.showsCameraControls = NO; ?

+7
ios objective-c uiimagepickercontroller crop
source share
3 answers

Just post it as an answer, but I'm not sure the poster will ask for an edited image.

When you take a picture using the UIImagePickerController, the collector displays only the portion of the image that is actually captured when shooting, as shown in the figure below, where the black lines are the iPhone screen.

enter image description here

The image you get from the camera is a full-size image, but only the center of the image with the width or height of the image, the maximum screen size is on the screen.

All you have to do is get the center from the image as shown above, and you have the exact image that you had on your screen.

+8
source share

You can get the original or cropped image depending on the key for the information dictionary.

pickedImageEdited will be the cropped image you are looking for, and pickedImageOriginal will be the full source image.

 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *pickedImageOriginal = [info objectForKey:UIImagePickerControllerOriginalImage]; UIImage *pickedImageEdited = [info objectForKey:UIImagePickerControllerEditedImage]; //do your stuff [self dismissViewControllerAnimated:YES completion:nil]; } 
+7
source share

After Wim's answer, I was able to find a solution similar to this:

 UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; UIGraphicsBeginImageContext(CGSizeMake(720, 960)); [image drawInRect: CGRectMake(0, 0, 720, 960)]; UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGRect cropRect = CGRectMake(40, 0, 640, 960); CGImageRef imageRef = CGImageCreateWithImageInRect([smallImage CGImage], cropRect); [self.imageView setImage:[UIImage imageWithCGImage:imageRef]]; 

As you can see, this is done for the iPhone’s 3.5-inch screen, and it seems to work, but the code is device dependent or is there a better solution?

+4
source share

All Articles