iphone – imageOrientation返回Up,但照片显示颠倒
发布时间:2020-12-14 19:54:26 所属栏目:百科 来源:网络整理
导读:我正在从iPad照片库中抓取一些照片.我在横向模式下拍了几张测试照片,左边是iPad主页按钮,右边是. 所以在我的应用程序中,由于某种原因,一些照片显示为颠倒.我检查了他们的imageOrientation属性,它们都是0(意思是它们是UIImageOrientationUp).这意味着我甚至不
我正在从iPad照片库中抓取一些照片.我在横向模式下拍了几张测试照片,左边是iPad主页按钮,右边是.
所以在我的应用程序中,由于某种原因,一些照片显示为颠倒.我检查了他们的imageOrientation属性,它们都是0(意思是它们是UIImageOrientationUp).这意味着我甚至不知道我需要旋转哪些照片. 发生了什么,我怎么知道哪些照片需要旋转?谢谢 解决方法
UIImage有一个属性imageOrientation,它指示UIImageView和其他UIImage使用者旋转原始图像数据.这个标志很有可能被保存到上传的jpeg图像中的exif数据,但是用来查看它的程序并没有遵守该标志.
要在上传时旋转UIImage以正确显示,您可以使用如下类别: UIImagefixOrientation.h @interface UIImage (fixOrientation) - (UIImage *)fixOrientation; @end UIImagefixOrientation.m @implementation UIImage (fixOrientation) - (UIImage *)fixOrientation { // No-op if the orientation is already correct if (self.imageOrientation == UIImageOrientationUp) return self; // We need to calculate the proper transformation to make the image upright. // We do it in 2 steps: Rotate if Left/Right/Down,and then flip if Mirrored. CGAffineTransform transform = CGAffineTransformIdentity; switch (self.imageOrientation) { case UIImageOrientationDown: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform,self.size.width,self.size.height); transform = CGAffineTransformRotate(transform,M_PI); break; case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: transform = CGAffineTransformTranslate(transform,0); transform = CGAffineTransformRotate(transform,M_PI_2); break; case UIImageOrientationRight: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform,self.size.height); transform = CGAffineTransformRotate(transform,-M_PI_2); break; } switch (self.imageOrientation) { case UIImageOrientationUpMirrored: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform,0); transform = CGAffineTransformScale(transform,-1,1); break; case UIImageOrientationLeftMirrored: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform,self.size.height,1); break; } // Now we draw the underlying CGImage into a new context,applying the transform // calculated above. CGContextRef ctx = CGBitmapContextCreate(NULL,CGImageGetBitsPerComponent(self.CGImage),CGImageGetColorSpace(self.CGImage),CGImageGetBitmapInfo(self.CGImage)); CGContextConcatCTM(ctx,transform); switch (self.imageOrientation) { case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: case UIImageOrientationRight: case UIImageOrientationRightMirrored: // Grr... CGContextDrawImage(ctx,CGRectMake(0,self.size.width),self.CGImage); break; default: CGContextDrawImage(ctx,self.size.height),self.CGImage); break; } // And now we just create a new UIImage from the drawing context CGImageRef cgimg = CGBitmapContextCreateImage(ctx); UIImage *img = [UIImage imageWithCGImage:cgimg]; CGContextRelease(ctx); CGImageRelease(cgimg); return img; } @end 并在捕获图像保存期间调用此函数或从库中选择图像. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |