为了账号安全,请及时绑定邮箱和手机立即绑定

iOS图像方向行为异常

iOS图像方向行为异常

呼啦一阵风 2019-11-07 13:08:44
在过去的几周中,我一直在用Objective-C处理图像,并注意到许多奇怪的行为。首先,像其他许多人一样,我一直遇到这个问题,即用相机拍摄的图像(或用别人的相机拍摄并发给我的彩信)旋转了90度。我不确定为什么在世界上发生这种情况(因此提出了我的问题),但是我能够提出一个廉价的解决方案。我这次的问题是为什么会这样?苹果为什么要旋转图像?当我将相机正面朝上拍摄照片时,除非执行上述代码,否则在保存照片时会旋转保存。现在,我的解决方法在几天前还可以。我的应用程序修改了图像的各个像素,特别是PNG的Alpha通道(因此,对于我的方案,任何JPEG转换都从窗口中抛出)。几天前,我注意到即使使用了变通方法代码,即使在应用中正确显示了图像,当我的算法修改图像的各个像素时,它仍认为图像是旋转的。因此,它不会修改图像顶部的像素,而是会修改图像侧面的像素(因为它认为应该旋转像素)!我不知道如何旋转内存中的图像-理想情况下,我宁愿只擦除该imageOrientation标志。这也让我感到困惑。当我拍摄照片时,将imageOrientation其设置为3。我的解决方法代码足够聪明,可以实现并翻转它,因此用户不会注意到。另外,我将图像保存到库中的代码实现了这一点,将其翻转然后保存,以使其正确显示在相机胶卷中。该代码如下所示:NSData* pngdata = UIImagePNGRepresentation (self.workingImage); //PNG wrap UIImage* img = [self rotateImageAppropriately:[UIImage imageWithData:pngdata]];   UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);当我将此新保存的图像加载到我的应用程序中时,其imageOrientation值为0-正是我要查看的图像,甚至不需要运行旋转解决方法(请注意:从互联网加载图像而不是使用相机拍摄的图像时) ,imageOrientation则始终为0,从而获得完美的行为)。由于某种原因,我的保存代码似乎清除了该imageOrientation标志。我希望窃取该代码,并在用户拍摄照片并将其添加到应用程序后立即使用它擦除我的imageOrientation,但它似乎不起作用。有UIImageWriteToSavedPhotosAlbum什么特别之处imageOrientation吗?解决此问题的最佳方法是imageOrientation在用户完成拍摄图像后立即吹走。我认为苹果的旋转行为是有原因的,对吧?少数人认为这是苹果的缺陷。(...如果您还没有迷路,请注意...)注2:当我拍摄水平照片时,一切似乎都可以正常工作,就像从互联网上拍摄的照片一样)在对这个问题进行了所有评论之后,这就是我的想法... iPhone拍摄了一张图像,并说“要正确显示此图像,必须将其旋转90度”。此信息将在EXIF数据中。(我不知道为什么它需要旋转90度,而不是默认为笔直垂直)。从这里开始,Gmail足够聪明,可以读取和分析该EXIF数据并正确显示。但是,Windows不够智能,无法读取EXIF数据,因此无法正确显示图像。我的假设正确吗?
查看完整描述

3 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

import Darwin


class func rotateCameraImageToProperOrientation(imageSource : UIImage, maxResolution : CGFloat) -> UIImage {


    let imgRef = imageSource.CGImage;


    let width = CGFloat(CGImageGetWidth(imgRef));

    let height = CGFloat(CGImageGetHeight(imgRef));


    var bounds = CGRectMake(0, 0, width, height)


    var scaleRatio : CGFloat = 1

    if (width > maxResolution || height > maxResolution) {


        scaleRatio = min(maxResolution / bounds.size.width, maxResolution / bounds.size.height)

        bounds.size.height = bounds.size.height * scaleRatio

        bounds.size.width = bounds.size.width * scaleRatio

    }


    var transform = CGAffineTransformIdentity

    let orient = imageSource.imageOrientation

    let imageSize = CGSizeMake(CGFloat(CGImageGetWidth(imgRef)), CGFloat(CGImageGetHeight(imgRef)))



    switch(imageSource.imageOrientation) {

    case .Up :

        transform = CGAffineTransformIdentity


    case .UpMirrored :

        transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);

        transform = CGAffineTransformScale(transform, -1.0, 1.0);


    case .Down :

        transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);

        transform = CGAffineTransformRotate(transform, CGFloat(M_PI));


    case .DownMirrored :

        transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);

        transform = CGAffineTransformScale(transform, 1.0, -1.0);


    case .Left :

        let storedHeight = bounds.size.height

        bounds.size.height = bounds.size.width;

        bounds.size.width = storedHeight;

        transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);

        transform = CGAffineTransformRotate(transform, 3.0 * CGFloat(M_PI) / 2.0);


    case .LeftMirrored :

        let storedHeight = bounds.size.height

        bounds.size.height = bounds.size.width;

        bounds.size.width = storedHeight;

        transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);

        transform = CGAffineTransformScale(transform, -1.0, 1.0);

        transform = CGAffineTransformRotate(transform, 3.0 * CGFloat(M_PI) / 2.0);


    case .Right :

        let storedHeight = bounds.size.height

        bounds.size.height = bounds.size.width;

        bounds.size.width = storedHeight;

        transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);

        transform = CGAffineTransformRotate(transform, CGFloat(M_PI) / 2.0);


    case .RightMirrored :

        let storedHeight = bounds.size.height

        bounds.size.height = bounds.size.width;

        bounds.size.width = storedHeight;

        transform = CGAffineTransformMakeScale(-1.0, 1.0);

        transform = CGAffineTransformRotate(transform, CGFloat(M_PI) / 2.0);


    default : ()

    }


    UIGraphicsBeginImageContext(bounds.size)

    let context = UIGraphicsGetCurrentContext()


    if orient == .Right || orient == .Left {

        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);


    let imageCopy = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();


    return imageCopy;

}


查看完整回答
反对 回复 2019-11-07
  • 3 回答
  • 0 关注
  • 586 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信