Crop automatically with a camera like instagram

I want to take a square picture from an application such as instagram. However, I do not know how to do this. I know that y, the image still needs to be cropped, and the camera captures the whole image all the time, only I crop it “alive” in the view. When an image is captured, it is still the whole image and still needs to be cropped. Only the user does not see how this happens in the background. After you capture the image using your device’s camera, put it in a UIImage instance, then write a method that this UIIMage will take and use CGRefImageContext to resize it, or overwrite the original UIImage instance if you no longer need the original. I don’t understand how to do this. Is there a website that shows how to do this? or is there an easier way to do this.

+4
source share
1 answer

First you need to take a picture with the camera, so add this code where you want to take a picture.

UIImagePickerController *pickerController = [[UIImagePickerController alloc] init]; if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { pickerController.sourceType = UIImagePickerControllerSourceTypeCamera; pickerController.delegate = self; [self presentViewController:pickerController animated:YES completion:nil]; } 

Then you need to implement delegation methods. Your class must match the UIImagePickerControllerDelegate documented here . In the imagePickerController: didFinishPickingMediaWithInfo: you can take a taken photo in UIImage with:

 UIImage *imageTaked = info[UIImagePickerControllerOriginalImage]; 

If you want, at this point you can take the metadata of the taken photo using

 info [UIImagePickerControllerMediaMetadata]; 

Finally, when you have a photo in UIImage, you can crop the image as follows:

 CGFloat x, y, side; //We will see how to calculate those values later. UIImage* imageCropped; CGRect cropRect = CGRectMake(x,y,side,side); CGImageRef imageRef = CGImageCreateWithImageInRect([imageTaked CGImage], cropRect); imageCropped = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); 

So, here it is important how to calculate cropRect in order to cut the center square of the UIImage. To do this, you can calculate x , y and side as follows:

 side = MIN(imageTaked.size.width, imageTaked.size.height x = imageTaked.size.width / 2 - side / 2; y = imageTaked.size.height / 2 - side / 2; 

So, to summarize, all the code inside the imagePickerController: didFinishPickingMediaWithInfo: method results in:

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *imageTaked = info[UIImagePickerControllerOriginalImage]; //You can take the metadata here => info [UIImagePickerControllerMediaMetadata]; UIImage* imageCropped; CGFloat side = MIN(imageTaked.size.width, imageTaked.size.height); CGFloat x = imageTaked.size.width / 2 - side / 2; CGFloat y = imageTaked.size.height / 2 - side / 2; CGRect cropRect = CGRectMake(x,y,side,side); CGImageRef imageRef = CGImageCreateWithImageInRect([imageTaked CGImage], cropRect); imageCropped = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); } 

EDIT

If you want to send your photo to another device, the photo may be rotated. This is because Apple uses exif metadata for images, but this is not the case on other platforms. To solve this problem, simply use this method to set the appropriate metadata information:

 -(UIImage*)getImageWithCorrectFiltersFromOriginalImage:(UIImage *)originalImage { UIImageOrientation orientation = UIImageOrientationUp; NSNumber* orientationValue = [originalImage valueForProperty:@"ALAssetPropertyOrientation"]; if (orientationValue) { orientation = [orientationValue intValue]; } ALAssetRepresentation *assetRep = [originalImage defaultRepresentation]; CGImageRef imageRef = [assetRep fullResolutionImage]; return [UIImage imageWithCGImage:imageRef scale:[assetRep scale] orientation:orientation]; } 

Hope this helps! and let me know if something goes wrong during the copy-paste process, it's 2:00 AM here, and I'm a zombie man now!

+10
source

Source: https://habr.com/ru/post/1495066/


All Articles