ios – 相机图像旋转问题

ios – 相机图像旋转问题,第1张

概述我在这里遇到一个非常奇怪的问题.当我以纵向模式单击图像并上传它然后再次获取它时,显示逆时针旋转90度.但是当我在相机胶卷中看到它时,它会以正确的方向显示.我已经尝试了几乎所有可能的链接/代码来解决这个问题,但似乎没有任何帮助.我以JPEG格式保存图像.请帮助这个人. 提前致谢!! 解决方法是在UI Image上创建一个类别,并根据其元数据EXIF缩放和旋转图像. 这是一段神奇的代码: - (UII 我在这里遇到一个非常奇怪的问题.当我以纵向模式单击图像并上传它然后再次获取它时,显示逆时针旋转90度.但是当我在相机胶卷中看到它时,它会以正确的方向显示.我已经尝试了几乎所有可能的链接/代码来解决这个问题,但似乎没有任何帮助.我以JPEG格式保存图像.请帮助这个人.
提前致谢!!解决方法 解决方法是在UI Image上创建一个类别,并根据其元数据EXIF缩放和旋转图像.

这是一段神奇的代码:

- (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,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            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            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);            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,M_PI / 2.0);            break;        case UIImageOrIEntationRight: //EXIF = 8            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,-height);    }    CGContextConcatCTM(context,transform);    CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(0,height),imgRef);    UIImage *imagecopy = UIGraphicsGetimageFromCurrentimageContext();    UIGraphicsEndImageContext();    return imagecopy;}
总结

以上是内存溢出为你收集整理的ios – 相机图像旋转问题全部内容,希望文章能够帮你解决ios – 相机图像旋转问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1100837.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存