OStack程序员社区-中国程序员成长平台

标题: ios - 从选定区域中的 uiimagepickerviewcontroller 裁剪图像 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 02:52
标题: ios - 从选定区域中的 uiimagepickerviewcontroller 裁剪图像

如何在捕获图像时从选定区域中的 UIImagePickerController 裁剪图像。覆盖 View 到 UIImagePickerController



Best Answer-推荐答案


代码下方

-(UIImage *)centerCropImageUIImage *)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)imagePickerControllerUIImagePickerController *)picker didFinishPickingMediaWithInfoNSDictionary *)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];
}

关于ios - 从选定区域中的 uiimagepickerviewcontroller 裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26628169/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4