gpt4 book ai didi

ios - 在 swift 中捕获图像后,ipad 中图像的方向发生变化

转载 作者:行者123 更新时间:2023-11-29 05:53:06 25 4
gpt4 key购买 nike

我想在从相机捕获的图像上添加标记图像,然后快速保存到照片库,但图像在照片库中保存的方向错误,所以请建议我如何解决此问题。我想在 iPhone 上以全速率模式保存图像,在 iPad 上以横向模式保存图像。所以请指出我哪里错了。我的代码在那里:{

    if let videoConnection = stillImageOutput.connection(with: AVMediaType.video) {

stillImageOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (CMSampleBuffer, Error) in
if let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(CMSampleBuffer!) {

if let cameraImage = UIImage(data: imageData) {



if let img2 = self.imgWaterMark.image {



// let rect = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)

let rect = CGRect(x: 0, y: 0, width: (self.previewLayer?.frame.size.width)! , height: (self.previewLayer?.frame.size.height)!)

UIGraphicsBeginImageContextWithOptions((self.previewLayer?.frame.size)!, true, 180)
let context = UIGraphicsGetCurrentContext()

context?.setFillColor(UIColor.white.cgColor)
context?.fill(rect)

cameraImage.draw(in: rect, blendMode: .normal, alpha: 1)
img2.draw(in:self.imgWaterMark.frame , blendMode: .normal, alpha: 1)
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

// let img = result

//let finalimage = self.rotateCameraImageToProperOrientation(imageSource: cameraImage)
UIImageWriteToSavedPhotosAlbum(result ?? cameraImage, nil, nil, nil)
// self.textToImage(drawText:self.lblTitle!.text! as NSString, inImage: result ?? cameraImage, atPoint: CGPoint(x: 0, y: 110))

AppSharedData.sharedInstance.showAlert(title: "", message: "Image saved", viewController: self, actions: ["OK"], callBack: { (action) in

})
}
}
}
})
}
}

最佳答案

我使用下面的代码来获取图像的方向并转换为所需的方向。请看看您是否可以使用它。

img_Photo.image = self.imageOrientation((img_Photo?.image)!)


//*******************************
func imageOrientation(_ src:UIImage)->UIImage {
print(src.imageOrientation.rawValue)
print(src.imageOrientation.hashValue)
if src.imageOrientation == UIImageOrientation.up {
return src
}/*else if src.imageOrientation == UIImageOrientation.down {
return src
}else if src.imageOrientation == UIImageOrientation.left {
return src
}else if src.imageOrientation == UIImageOrientation.right {
return src
}*/

var transform: CGAffineTransform = CGAffineTransform.identity
switch src.imageOrientation {
case UIImageOrientation.up, UIImageOrientation.upMirrored:
transform = transform.translatedBy(x: src.size.width, y: src.size.height)
transform = transform.rotated(by: CGFloat(M_PI))
break
case UIImageOrientation.down, UIImageOrientation.downMirrored:
transform = transform.translatedBy(x: src.size.width, y: src.size.height)
transform = transform.rotated(by: CGFloat(M_PI))
break
case UIImageOrientation.left, UIImageOrientation.leftMirrored:
transform = transform.translatedBy(x: src.size.width, y: 0)
transform = transform.rotated(by: CGFloat(M_PI_2))
break
case UIImageOrientation.right, UIImageOrientation.rightMirrored:
transform = transform.translatedBy(x: 0, y: src.size.height)
transform = transform.rotated(by: CGFloat(-M_PI_2))
break

}

switch src.imageOrientation {
case UIImageOrientation.upMirrored, UIImageOrientation.downMirrored:
transform.translatedBy(x: src.size.width, y: 0)
transform.scaledBy(x: -1, y: 1)
break
case UIImageOrientation.leftMirrored, UIImageOrientation.rightMirrored:
transform.translatedBy(x: src.size.height, y: 0)
transform.scaledBy(x: -1, y: 1)
case UIImageOrientation.up, UIImageOrientation.down, UIImageOrientation.left, UIImageOrientation.right:
break
}

let ctx:CGContext = CGContext(data: nil, width: Int(src.size.width), height: Int(src.size.height), bitsPerComponent: (src.cgImage)!.bitsPerComponent, bytesPerRow: 0, space: (src.cgImage)!.colorSpace!, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!

ctx.concatenate(transform)

switch src.imageOrientation {
case UIImageOrientation.left, UIImageOrientation.leftMirrored, UIImageOrientation.right, UIImageOrientation.rightMirrored:
ctx.draw(src.cgImage!, in: CGRect(x: 0, y: 0, width: src.size.height, height: src.size.width))
break
default:
ctx.draw(src.cgImage!, in: CGRect(x: 0, y: 0, width: src.size.width, height: src.size.height))
break
}

let cgimg:CGImage = ctx.makeImage()!
let img:UIImage = UIImage(cgImage: cgimg)

return img
}

关于ios - 在 swift 中捕获图像后,ipad 中图像的方向发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55474262/

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