1

我在收割期间遇到了问题。在横向拍摄时效果很好,但在纵向拍摄时不会裁剪。我希望用户也只拍摄肖像。我的代码中是否有任何问题导致无法纵向工作,或者我应该添加代码吗?第一个代码用于启动相机,第二个 imagepickercontroller 用于裁剪。

-(IBAction)cameraButtonPressed:(id)sender
{

    UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];

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

}


- (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 / 320 - side / 320;
        CGFloat y = imageTaked.size.height / 320 - side / 320;

        CGRect cropRect = CGRectMake(x,y,side,side);
        CGImageRef imageRef = CGImageCreateWithImageInRect([imageTaked CGImage], cropRect);
        imageCropped = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);
    }
}
4

0 回答 0