I want to add markar image on captured image from camera , then saving to photo library in swift , but the image saved with wrong orientation in photo library , so please suggest how I will fixed this issue . I want to save image in pot-rate mode for iPhone and lanscape mode for iPad . so please suggest where I am wrong . my code is there: {
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
})
}
}
}
})
}
}