iOS Swift 3 图片向左旋转 90 度
iOS Swift 3 Image rotated 90 degrees Left
在我的应用程序中,我有一个功能可以从 camera
拍照并将其上传到服务器。我的问题是,当我从服务器收到响应时,图像向左侧旋转了 90 度。这是我试过的代码:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
self.PickerFrom = self.PickerFrom2
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
{
self.AttachmentImageView.image = pickedImage
}
dismiss(animated: true, completion: nil)
uploadGalleryImage(image: self.AttachmentImageView.image!)
}
func uploadGalleryImage( image:UIImage)
{
let imageData: NSData = UIImagePNGRepresentation(image)! as NSData
let base64String = imageData.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
self.AttachedDocumentURL = "data:image/png;base64,\(base64String)"
}
AttachedDocumentURL
包含捕获图像的 base64 字符串。
在上传到服务器之前试试这个(重置图片方向)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
self.PickerFrom = self.PickerFrom2
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
{
self.AttachmentImageView.image = pickedImage
}
dismiss(animated: true, completion: nil)
if let updatedImage = self.AttachmentImageView.image?.updateImageOrientionUpSide() {
uploadGalleryImage(image: updatedImage)
} else {
uploadGalleryImage(image: self.AttachmentImageView.image!)
}
}
func uploadGalleryImage( image:UIImage)
{
let imageData: NSData = UIImagePNGRepresentation(image)! as NSData
let base64String = imageData.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
self.AttachedDocumentURL = "data:image/png;base64,\(base64String)"
}
// Image extension
extension UIImage {
func updateImageOrientionUpSide() -> UIImage? {
if self.imageOrientation == .up {
return self
}
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
if let normalizedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext() {
UIGraphicsEndImageContext()
return normalizedImage
}
UIGraphicsEndImageContext()
return nil
}
}
使用这个常用函数从任何角度修正图像的上部方向。
extension UIImage {
func fixImageOrientation() -> UIImage? {
if (self.imageOrientation == .up) {
return self
}
var transform: CGAffineTransform = CGAffineTransform.identity
if ( self.imageOrientation == .left || self.imageOrientation == .leftMirrored ) {
transform = transform.translatedBy(x: self.size.width, y: 0)
transform = transform.rotated(by: CGFloat(Double.pi / 2.0))
} else if ( self.imageOrientation == .right || self.imageOrientation == .rightMirrored ) {
transform = transform.translatedBy(x: 0, y: self.size.height);
transform = transform.rotated(by: CGFloat(-Double.pi / 2.0));
} else if ( self.imageOrientation == .down || self.imageOrientation == .downMirrored ) {
transform = transform.translatedBy(x: self.size.width, y: self.size.height)
transform = transform.rotated(by: CGFloat(Double.pi))
} else if ( self.imageOrientation == .upMirrored || self.imageOrientation == .downMirrored ) {
transform = transform.translatedBy(x: self.size.width, y: 0)
transform = transform.scaledBy(x: -1, y: 1)
} else if ( self.imageOrientation == .leftMirrored || self.imageOrientation == .rightMirrored ) {
transform = transform.translatedBy(x: self.size.height, y: 0);
transform = transform.scaledBy(x: -1, y: 1);
}
if let context: CGContext = CGContext(data: nil, width: Int(self.size.width), height: Int(self.size.height),
bitsPerComponent: self.cgImage!.bitsPerComponent, bytesPerRow: 0,
space: self.cgImage!.colorSpace!,
bitmapInfo: self.cgImage!.bitmapInfo.rawValue) {
context.concatenate(transform)
if ( self.imageOrientation == UIImageOrientation.left ||
self.imageOrientation == UIImageOrientation.leftMirrored ||
self.imageOrientation == UIImageOrientation.right ||
self.imageOrientation == UIImageOrientation.rightMirrored ) {
context.draw(self.cgImage!, in: CGRect(x: 0,y: 0,width: self.size.height,height: self.size.width))
} else {
context.draw(self.cgImage!, in: CGRect(x: 0,y: 0,width: self.size.width,height: self.size.height))
}
if let contextImage = context.makeImage() {
return UIImage(cgImage: contextImage)
}
}
return nil
}
}
Swift 4.1 兼容。
此扩展避免在 iOS 设备端发送图像 exif 数据。
extension UIImage {
func fixOrientation() -> UIImage? {
if (imageOrientation == .up) { return self }
var transform = CGAffineTransform.identity
switch imageOrientation {
case .left, .leftMirrored:
transform = transform.translatedBy(x: size.width, y: 0.0)
transform = transform.rotated(by: .pi / 2.0)
case .right, .rightMirrored:
transform = transform.translatedBy(x: 0.0, y: size.height)
transform = transform.rotated(by: -.pi / 2.0)
case .down, .downMirrored:
transform = transform.translatedBy(x: size.width, y: size.height)
transform = transform.rotated(by: .pi)
default:
break
}
switch imageOrientation {
case .upMirrored, .downMirrored:
transform = transform.translatedBy(x: size.width, y: 0.0)
transform = transform.scaledBy(x: -1.0, y: 1.0)
case .leftMirrored, .rightMirrored:
transform = transform.translatedBy(x: size.height, y: 0.0)
transform = transform.scaledBy(x: -1.0, y: 1.0)
default:
break
}
guard let cgImg = cgImage else { return nil }
if let context = CGContext(data: nil,
width: Int(size.width), height: Int(size.height),
bitsPerComponent: cgImg.bitsPerComponent,
bytesPerRow: 0, space: cgImg.colorSpace!,
bitmapInfo: cgImg.bitmapInfo.rawValue) {
context.concatenate(transform)
if imageOrientation == .left || imageOrientation == .leftMirrored ||
imageOrientation == .right || imageOrientation == .rightMirrored {
context.draw(cgImg, in: CGRect(x: 0.0, y: 0.0, width: size.height, height: size.width))
} else {
context.draw(cgImg, in: CGRect(x: 0.0 , y: 0.0, width: size.width, height: size.height))
}
if let contextImage = context.makeImage() {
return UIImage(cgImage: contextImage)
}
}
return nil
}
}
在我的应用程序中,我有一个功能可以从 camera
拍照并将其上传到服务器。我的问题是,当我从服务器收到响应时,图像向左侧旋转了 90 度。这是我试过的代码:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
self.PickerFrom = self.PickerFrom2
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
{
self.AttachmentImageView.image = pickedImage
}
dismiss(animated: true, completion: nil)
uploadGalleryImage(image: self.AttachmentImageView.image!)
}
func uploadGalleryImage( image:UIImage)
{
let imageData: NSData = UIImagePNGRepresentation(image)! as NSData
let base64String = imageData.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
self.AttachedDocumentURL = "data:image/png;base64,\(base64String)"
}
AttachedDocumentURL
包含捕获图像的 base64 字符串。
在上传到服务器之前试试这个(重置图片方向)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
self.PickerFrom = self.PickerFrom2
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
{
self.AttachmentImageView.image = pickedImage
}
dismiss(animated: true, completion: nil)
if let updatedImage = self.AttachmentImageView.image?.updateImageOrientionUpSide() {
uploadGalleryImage(image: updatedImage)
} else {
uploadGalleryImage(image: self.AttachmentImageView.image!)
}
}
func uploadGalleryImage( image:UIImage)
{
let imageData: NSData = UIImagePNGRepresentation(image)! as NSData
let base64String = imageData.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
self.AttachedDocumentURL = "data:image/png;base64,\(base64String)"
}
// Image extension
extension UIImage {
func updateImageOrientionUpSide() -> UIImage? {
if self.imageOrientation == .up {
return self
}
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
if let normalizedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext() {
UIGraphicsEndImageContext()
return normalizedImage
}
UIGraphicsEndImageContext()
return nil
}
}
使用这个常用函数从任何角度修正图像的上部方向。
extension UIImage {
func fixImageOrientation() -> UIImage? {
if (self.imageOrientation == .up) {
return self
}
var transform: CGAffineTransform = CGAffineTransform.identity
if ( self.imageOrientation == .left || self.imageOrientation == .leftMirrored ) {
transform = transform.translatedBy(x: self.size.width, y: 0)
transform = transform.rotated(by: CGFloat(Double.pi / 2.0))
} else if ( self.imageOrientation == .right || self.imageOrientation == .rightMirrored ) {
transform = transform.translatedBy(x: 0, y: self.size.height);
transform = transform.rotated(by: CGFloat(-Double.pi / 2.0));
} else if ( self.imageOrientation == .down || self.imageOrientation == .downMirrored ) {
transform = transform.translatedBy(x: self.size.width, y: self.size.height)
transform = transform.rotated(by: CGFloat(Double.pi))
} else if ( self.imageOrientation == .upMirrored || self.imageOrientation == .downMirrored ) {
transform = transform.translatedBy(x: self.size.width, y: 0)
transform = transform.scaledBy(x: -1, y: 1)
} else if ( self.imageOrientation == .leftMirrored || self.imageOrientation == .rightMirrored ) {
transform = transform.translatedBy(x: self.size.height, y: 0);
transform = transform.scaledBy(x: -1, y: 1);
}
if let context: CGContext = CGContext(data: nil, width: Int(self.size.width), height: Int(self.size.height),
bitsPerComponent: self.cgImage!.bitsPerComponent, bytesPerRow: 0,
space: self.cgImage!.colorSpace!,
bitmapInfo: self.cgImage!.bitmapInfo.rawValue) {
context.concatenate(transform)
if ( self.imageOrientation == UIImageOrientation.left ||
self.imageOrientation == UIImageOrientation.leftMirrored ||
self.imageOrientation == UIImageOrientation.right ||
self.imageOrientation == UIImageOrientation.rightMirrored ) {
context.draw(self.cgImage!, in: CGRect(x: 0,y: 0,width: self.size.height,height: self.size.width))
} else {
context.draw(self.cgImage!, in: CGRect(x: 0,y: 0,width: self.size.width,height: self.size.height))
}
if let contextImage = context.makeImage() {
return UIImage(cgImage: contextImage)
}
}
return nil
}
}
Swift 4.1 兼容。 此扩展避免在 iOS 设备端发送图像 exif 数据。
extension UIImage {
func fixOrientation() -> UIImage? {
if (imageOrientation == .up) { return self }
var transform = CGAffineTransform.identity
switch imageOrientation {
case .left, .leftMirrored:
transform = transform.translatedBy(x: size.width, y: 0.0)
transform = transform.rotated(by: .pi / 2.0)
case .right, .rightMirrored:
transform = transform.translatedBy(x: 0.0, y: size.height)
transform = transform.rotated(by: -.pi / 2.0)
case .down, .downMirrored:
transform = transform.translatedBy(x: size.width, y: size.height)
transform = transform.rotated(by: .pi)
default:
break
}
switch imageOrientation {
case .upMirrored, .downMirrored:
transform = transform.translatedBy(x: size.width, y: 0.0)
transform = transform.scaledBy(x: -1.0, y: 1.0)
case .leftMirrored, .rightMirrored:
transform = transform.translatedBy(x: size.height, y: 0.0)
transform = transform.scaledBy(x: -1.0, y: 1.0)
default:
break
}
guard let cgImg = cgImage else { return nil }
if let context = CGContext(data: nil,
width: Int(size.width), height: Int(size.height),
bitsPerComponent: cgImg.bitsPerComponent,
bytesPerRow: 0, space: cgImg.colorSpace!,
bitmapInfo: cgImg.bitmapInfo.rawValue) {
context.concatenate(transform)
if imageOrientation == .left || imageOrientation == .leftMirrored ||
imageOrientation == .right || imageOrientation == .rightMirrored {
context.draw(cgImg, in: CGRect(x: 0.0, y: 0.0, width: size.height, height: size.width))
} else {
context.draw(cgImg, in: CGRect(x: 0.0 , y: 0.0, width: size.width, height: size.height))
}
if let contextImage = context.makeImage() {
return UIImage(cgImage: contextImage)
}
}
return nil
}
}