在我的应用中,我将从用户的图片库中导入所选照片,并将其用作缩略图/在UIImageView
s中显示它们。它们将被导入并保存到用户的文档文件夹中。
下面使用的代码,但是图像在保存之前会自动旋转,我不确定为什么。
例如,此图像旋转了180°:https://imgur.com/a/r2xm0if,其他一些宽度大于高度的图像也是如此。我不确定为什么,但并非所有图像都这样旋转。有些旋转了90°,有些则完全没有。
它们在磁盘上旋转-不仅在UIImageView
中旋转,因此我知道这是如何设置的。
importPicture()。
@objc func importPicture() {
let picker = UIImagePickerController()
picker.mediaTypes = [kUTTypeMovie as String, kUTTypeImage as String]
picker.allowsEditing = true
picker.delegate = self
present(picker, animated: true)
}
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let documentsDirectoryURL = try! FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
// create a name for your image
let addedURL = UUID().uuidString
let fileURL = documentsDirectoryURL.appendingPathComponent("\(addedURL).png")
let videoURL2 = documentsDirectoryURL.appendingPathComponent("\(addedURL).mov")
let mediaType = info[UIImagePickerController.InfoKey.mediaType] as AnyObject
// Movie
if mediaType as! String == kUTTypeMovie as String {
let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as? URL
print("VIDEO URL: \(videoURL!)")
let myVideoVarData = try! Data(contentsOf: videoURL! as URL)
if !FileManager.default.fileExists(atPath: videoURL2.path) {
do {
try myVideoVarData.write(to: videoURL2)
print("Video Added Successfully")
print(videoURL2)
} catch {
print(error)
}
} else {
print("Video not added")
}
photosNew.append("\(addedURL).mov")
collectionView.reloadData()
}
// Image
if mediaType as! String == kUTTypeImage as String {
guard let image = info[.originalImage] as? UIImage else {
return
}
if let imageData = image.pngData() {
if !FileManager.default.fileExists(atPath: fileURL.path) {
do {
try imageData.write(to: fileURL)
print("Image Added Successfully")
print(fileURL)
} catch {
print(error)
}
} else {
print("Image Not Added")
}
}
photosNew.append("\(addedURL).png")
collectionView.reloadData()
}
答案 0 :(得分:1)
创建了一个UIImage
扩展名以将图像保持在原始位置,
extension UIImage {
func fixedOrientation() -> UIImage {
if imageOrientation == .up {
return self
}
var transform: CGAffineTransform = CGAffineTransform.identity
switch imageOrientation {
case .down, .downMirrored:
transform = transform.translatedBy(x: size.width, y: size.height)
transform = transform.rotated(by: CGFloat.pi)
case .left, .leftMirrored:
transform = transform.translatedBy(x: size.width, y: 0)
transform = transform.rotated(by: CGFloat.pi / 2)
case .right, .rightMirrored:
transform = transform.translatedBy(x: 0, y: size.height)
transform = transform.rotated(by: CGFloat.pi / -2)
case .up, .upMirrored:
break
}
switch imageOrientation {
case .upMirrored, .downMirrored:
transform.translatedBy(x: size.width, y: 0)
transform.scaledBy(x: -1, y: 1)
case .leftMirrored, .rightMirrored:
transform.translatedBy(x: size.height, y: 0)
transform.scaledBy(x: -1, y: 1)
case .up, .down, .left, .right:
break
}
if let cgImage = self.cgImage, let colorSpace = cgImage.colorSpace,
let ctx: CGContext = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) {
ctx.concatenate(transform)
switch imageOrientation {
case .left, .leftMirrored, .right, .rightMirrored:
ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.height, height: size.width))
default:
ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
}
if let ctxImage: CGImage = ctx.makeImage() {
return UIImage(cgImage: ctxImage)
} else {
return self
}
} else {
return self
}
}
}
要使用扩展程序的方法,请对您的image
执行以下操作,
let orientedImage = image?.fixedOrientation()
在您的情况下,在func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
方法内,
...
guard let image = info[.originalImage] as? UIImage else {
return
}
let orientedImage = image.fixedOrientation()
if let imageData = orientedImage.pngData() {
...