我正在尝试实现一个 iOS 相机视图,它可以拍摄方形照片(类似于 Instagram)。我的代码出现在下面。第一部分,其中框架高度设置为等于框架宽度,按预期工作,并且为用户提供了一个正方形的视图。当我尝试使用 CGImageCreateWithImageInRect 将框架(这是一个 CGRect 属性)应用于图像数据时,稍后会出现问题。我将框架矩形与图像一起传递给此方法。但结果并没有被裁剪成正方形。相反,图像保留了 iOS 相机的原始默认尺寸。有人可以告诉我我做错了什么吗?我对 Apple 文档的理解是 CGImageCreateWithImageInRect 应该从某个起始 x/y 坐标中选择形状为 Rect 的图像区域。但这并不
//Set the frame size to be square shaped
UIView *view = imagePicker.view;
frame = view.frame;
frame.size.height = frame.size.width;
view.frame = frame;
//Crop the image to the frame dimensions using CGImageCreateWithImageInRect
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.popoverController dismissPopoverAnimated:true];
NSString *mediaType = [info
objectForKey:UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:YES];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info
objectForKey:UIImagePickerControllerOriginalImage];
croppedImage = (__bridge UIImage *)(CGImageCreateWithImageInRect((__bridge CGImageRef)(image), frame));
imageView.image = croppedImage;
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
// Code here to support video if enabled
}
}