捕获图像时如何从所选区域中的UIImagePickerController
裁剪图像。 overlayview to UIImagePickerController
答案 0 :(得分:1)
代码
-(UIImage *)centerCropImage:(UIImage *)image
{
// Use smallest side length as crop square length
CGFloat squareLength = MIN(image.size.width, image.size.height);
// Center the crop area
CGRect clippedRect = CGRectMake((image.size.width - squareLength) / 2, (image.size.height - squareLength) / 2, squareLength, squareLength);
// Crop logic
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
UIImage * croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}
之后在imagePickerViewDelegate方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey: @"UIImagePickerControllerOriginalImage"];
[self centerCropImage:image]; //Just give your image here for cropping
yourImage.image=image;
picker.delegate =self;
[picker dismissViewControllerAnimated:YES completion:nil];
}