首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用UIImageOrientation在横屏模式下正确保存UIImage

用UIImageOrientation在横屏模式下正确保存UIImage
EN

Stack Overflow用户
提问于 2012-03-29 00:00:11
回答 1查看 3.2K关注 0票数 1

我正在接收AVCaptureSessionUIImage,并将它们设置为UIImageView的。纵向模式设置图像的方向正确,但一旦我使用横向模式,图像设置为顺时针旋转90度。

我做了一些研究,发现我可以通过这种方法使用UIImageOrientation来解决我的问题:

代码语言:javascript
复制
[UIImage imageWithCGImage:cgimageref scale:1 orientation:orientation]

但我不明白是怎么做到的。如果我查看所有图像上的UIImageOrientation,则在纵向和横向模式下,该值始终为3 (NSLog(@"%d",UIImageOrientation))。我不明白这对我有什么帮助。我是不是遗漏了什么?

以下是方向代码:

代码语言:javascript
复制
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
  self.prevLayer.frame = self.view.bounds;
  self.prevLayer.orientation = [[UIDevice currentDevice] orientation];
}

shouldAutorotateToInterfaceOrientation返回YES。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-30 11:29:35

解决方案需要一些时间才能找到。我必须向AVCaptureConnection添加方向

代码语言:javascript
复制
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections){
    for (AVCaptureInputPort *port in [connection inputPorts]){
        if ([[port mediaType] isEqual:AVMediaTypeVideo] ){
            videoConnection = connection;
            break;
        }
    }
    if (videoConnection) { break; }
}

下面这一行是修复方法:

代码语言:javascript
复制
if([videoConnection isVideoOrientationSupported]) {
    [videoConnection setVideoOrientation:[[UIDevice currentDevice] orientation]];
}

而且所有的图像在任何方向上都能正常工作。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9911390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档