提前致谢!!解决方法 解决方法是在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 – 相机图像旋转问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)