Jun*_* Gu 3 image uiimage ios uiimageorientation
我正在使用AVCapture +相机肖像模式来捕捉图像.当我显示捕获的图像时,它很好 - 看起来很好.但是当我转换为jpeg表示然后转换为base64字符串到服务器并且服务器存储它时,它已经"旋转"了.
所以我在发送到服务器之前检查了图像方向:它是UIImageOrientation.Right(所以有没有办法用肖像模式捕获图像,但捕获的图像方向是什么?好吧,我怀疑经过一些挖掘).在服务器获得图像后,它没有做任何事情,只是忽略了我猜的方向元数据.
因为我捕获的图像看起来很好,我想保留图像的样子.但是,我想将图像方向设置为向上.如果我只是设置图像方向,图像看起来不再正确.
那么有没有办法在不导致图像旋转的情况下设置方向,或者在将方向设置为向上之后,如何保持方向但是旋转实际图像以使其看起来正确?
- (UIImage *)removeRotationForImage:(UIImage*)image {
if (image.imageOrientation == UIImageOrientationUp) return image;
UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
[image drawInRect:(CGRect){0, 0, image.size}];
UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return normalizedImage;
}
Run Code Online (Sandbox Code Playgroud)
swrin版本的oldrinmendez答案
func removeRotationForImage(image: UIImage) -> UIImage {
if image.imageOrientation == UIImageOrientation.Up {
return image
}
UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
image.drawInRect(CGRect(origin: CGPoint(x: 0, y: 0), size: image.size))
let normalizedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return normalizedImage
}
Run Code Online (Sandbox Code Playgroud)