在我的应用中,我有一项功能可以从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
包含捕获图像的base64sting。
答案 0 :(得分:10)
在服务器上传之前尝试此操作(重置图像方向)
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
}
}
答案 1 :(得分:4)
使用此常用功能可以修复任何上方的图像方向。
xmlns:app="http://schemas.android.com/apk/res-auto"
答案 2 :(得分:3)
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
}
}