【问题标题】:Zooming a landscape image captured from camera to use it in portrait缩放从相机捕获的横向图像以将其用于纵向
【发布时间】:2012-07-05 11:36:15
【问题描述】:

在我的应用程序中,我使用UIImagePicker 从相机捕获image,而不是对其进行裁剪。但问题是,当我在横向模式下从相机拍摄图像时,在纵向模式下裁剪时,黑盒会显示在图像中。

图像是在横向模式下拍摄的 -

并且在使用该图像时,黑框显示在图像的上部和底部,如下图所示 -

但我想删除黑色部分,以便image 覆盖整个区域。

我该怎么做?

【问题讨论】:

  • 据我所知,图像选择器控制器无法做到这一点,因为默认情况下它会在宽高比适合模式下调整图像

标签: iphone objective-c ios ios5 camera


【解决方案1】:

您是否正在裁剪图像 CGImageCreateWithImageInRect([image CGImage], rect); 或者你能分享一下你是如何进行裁剪的吗?

当在横向模式下从UIImagePickerController 获取图像时,您需要检查 UIImage 的方向属性。即[image orientation];

如果方向值是UIInterfaceOrientationLandscapeLeft|UIInterfaceOrientationLandscapeRight,则在矩形宽度和高度之间互换。然后裁剪它。

【讨论】:

  • 感谢您的回复。但是你能解释一下如何在矩形宽度和高度之间互换吗?
  • 在横向时像这样改变CGImageCreateWithImageInRect([image CGImage], CGRectMake(rect.origin.x, rect.origin.y,rect.size.height,rect.size.width));我希望这会有所帮助。
  • 再次感谢您的回复。我已经尝试过您的代码,但它没有给出图像的方向。它显示警告“UIImage 可能无法响应方向”
【解决方案2】:
-(UIImage *)crop:(CGRect)rect Image:(UIImage *)originalImage ImageOrientation:(UIImageOrientation)imageOrientation
{

    CGImageRef imageRef = CGImageCreateWithImageInRect(originalImage.CGImage, rect);

    UIImage *result = [UIImage imageWithCGImage:imageRef scale:1.0f orientation:imageOrientation];

    CGImageRelease(imageRef);

    return result;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 2019-01-18
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    相关资源
    最近更新 更多