在屏幕上显示从相机捕获的图像时的混乱
Posted
技术标签:
【中文标题】在屏幕上显示从相机捕获的图像时的混乱【英文标题】:Confusion when presenting captured image from camera on screen 【发布时间】:2013-10-26 10:21:37 【问题描述】:我成功地从 iPhone 后置摄像头捕获图像并将其用作 UIImage。
图像可用后,我立即调用此方法:
- (void)putPhoto:(UIImage *)photo onTopOfPreview:(UIView *)preview
CALayer *photoLayer = [CALayer layer];
photoLayer.frame = CGRectMake(10, 10, 300, 300);
photoLayer.contents = (id)photo.CGImage;
[self.view.layer addSublayer:photoLayer];
// [preview.layer addSublayer:photoLayer];
但图像旋转了 -90˚。因为这是我第一次使用 CALayers,所以我尝试使用 UIImageView 呈现图像,如下所示:
- (void)putPhotoOnScreen:(UIImage *)photo
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
imageView.image = photo;
[self.view addSubview:imageView];
使用这种方法可以正确显示图像。所有这些都发生在纵向。
欢迎大家提出建议。
【问题讨论】:
【参考方案1】:试试这个方法
# pragma mark -
# pragma mark - Scale and Rotate according to Orientation
- (UIImage *)scaleAndRotateImage:(UIImage *)image
int kMaxResolution = 640; // Or whatever
CGImageRef imgRef = image.CGImage;
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGAffineTransform transform = CGAffineTransformIdentity;
CGRect bounds = CGRectMake(0, 0, width, height);
if (width > kMaxResolution || height > kMaxResolution)
CGFloat ratio = width/height;
if (ratio > 1)
bounds.size.width = kMaxResolution;
bounds.size.height = roundf(bounds.size.width / ratio);
else
bounds.size.height = kMaxResolution;
bounds.size.width = roundf(bounds.size.height * ratio);
CGFloat scaleRatio = bounds.size.width / width;
CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
CGFloat boundHeight;
UIImageOrientation orient = image.imageOrientation;
switch(orient)
case UIImageOrientationUp: //EXIF = 1
// landscape right
transform = CGAffineTransformIdentity;
break;
case UIImageOrientationUpMirrored: //EXIF = 2
transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
break;
case UIImageOrientationDown: //EXIF = 3
// landscape left
transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationDownMirrored: //EXIF = 4
transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
break;
case UIImageOrientationLeftMirrored: //EXIF = 5
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
case UIImageOrientationLeft: //EXIF = 6
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
case UIImageOrientationRightMirrored: //EXIF = 7
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeScale(-1.0, 1.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
break;
case UIImageOrientationRight: //EXIF = 8
// Portrait Mode
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
break;
default:
[NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
UIGraphicsBeginImageContext(bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft)
CGContextScaleCTM(context, -scaleRatio, scaleRatio);
CGContextTranslateCTM(context, -height, 0);
else
CGContextScaleCTM(context, scaleRatio, -scaleRatio);
CGContextTranslateCTM(context, 0, -height);
CGContextConcatCTM(context, transform);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageCopy;
【讨论】:
谢谢,我发现如果你将 UIImage 转换为 CGImage,关于方向的数据就会丢失。这也是解决方向问题的一种方法:***.com/a/15039609/1378140 使用我的方法你还可以设置图片分辨率,如果你想调整图片大小以上传到服务器等。【参考方案2】:如果你想改变图像的方向,可以通过设置方向枚举在 UIImageView 中设置 UIImage 来改变它。
UIImage *imageToDisplay =[UIImage imageWithCGImage:[originalImage CGImage]
scale:1.0
orientation: UIImageOrientationUp];
您可以将此图像设置为 UIImageView。
【讨论】:
以上是关于在屏幕上显示从相机捕获的图像时的混乱的主要内容,如果未能解决你的问题,请参考以下文章
使用啥控件从相机捕获图像并在 ui 上显示为带有事件 xamarin ios 的缩略图