gpt4 book ai didi

ios - 如何加载图像并根据它的方向 exif 数据旋转它并使用 UIImageOrientationUp exif 数据保存它

转载 作者:搜寻专家 更新时间:2023-11-01 06:12:58 25 4
gpt4 key购买 nike

如何加载图像并根据它的方向 exif 数据旋转它并使用UIImageOrientationUp exif 数据(或没有任何方向)保存它exif 数据)以便不处理 exif 方向数据的软件将正确显示图像?

最佳答案

加载图像就像这一行一样简单:

UIImage *image = [UIImage imageNamed:@"NameOfImageHere"];

或者如果你有图像数据:

NSData *imageData = [[NSData alloc] init];
UIImage *image = [UIImage imageWithData: imageData];

下面的方法将帮助您使用 UIImage 中的 exif 数据修复方向

- (UIImage *)imageByFixingOrientation {

UIImage *image = self;

if (image.imageOrientation == UIImageOrientationUp) return image;

CGAffineTransform transform = CGAffineTransformIdentity;

switch (image.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;

case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;

case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, image.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
break;
}

switch (image.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;

case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, image.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationUp:
case UIImageOrientationDown:
case UIImageOrientationLeft:
case UIImageOrientationRight:
break;
}

// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
CGImageGetBitsPerComponent(image.CGImage), 0,
CGImageGetColorSpace(image.CGImage),
CGImageGetBitmapInfo(image.CGImage));
CGContextConcatCTM(ctx, transform);
switch (image.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
break;

default:
CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.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;
}

关于ios - 如何加载图像并根据它的方向 exif 数据旋转它并使用 UIImageOrientationUp exif 数据保存它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51337016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com