菜鸟教程小白 发表于 2022-12-12 13:18:11

ios - 保存旋转的图片(swift3)


                                            <p><p><strong>图片不旋转</strong></p>

<p> <a href="/image/2Cih3.png" rel="noreferrer noopener nofollow"><img src="/image/2Cih3.png" alt="picture without rotation"/></a> </p>

<p><strong>图片旋转</strong></p>

<p> <a href="/image/W2FQY.png" rel="noreferrer noopener nofollow"><img src="/image/W2FQY.png" alt="picture with rotation"/></a> </p>

<p>我要做的就是拍摄旋转保存的照片。就像使用 avfoundation 构建的自定义相机旋转图片一样。现在我的代码拍摄照片并将它们保存到照片库,就像没有旋转的图片一样。 </p>

<pre><code>    import UIKit
import AVFoundation


class ViewController: UIViewController, AVCapturePhotoCaptureDelegate {
@IBOutlet var cameraDisplay: UIView!

var captureSession : AVCaptureSession!
var cameraOutput : AVCapturePhotoOutput!
var previewLayer : AVCaptureVideoPreviewLayer!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    captureSession = AVCaptureSession()
    captureSession.sessionPreset = AVCaptureSessionPresetPhoto
    cameraOutput = AVCapturePhotoOutput()


    let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

    if let input = try? AVCaptureDeviceInput(device: device) {
      if (captureSession.canAddInput(input)) {
            captureSession.addInput(input)
            if (captureSession.canAddOutput(cameraOutput)) {
                captureSession.addOutput(cameraOutput)


                previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                previewLayer.frame = CGRect(x: 10, y: 10, width: 700, height: 700)




                cameraDisplay.layer.addSublayer(previewLayer)
                captureSession.startRunning()

            }}}}


            func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
                if let error = error {
                  // we got back an error!
                  let ac = UIAlertController(title: &#34;Save error&#34;, message: error.localizedDescription, preferredStyle: .alert)
                  ac.addAction(UIAlertAction(title: &#34;OK&#34;, style: .default))
                  present(ac, animated: true)
                } else {

                  let ac = UIAlertController(title: &#34;Image Saved!&#34;, message: &#34;Your image has been saved to your photos.&#34;, preferredStyle: .alert)
                  ac.addAction(UIAlertAction(title: &#34;OK&#34;, style: .default))
                  present(ac, animated: true)
                }
            }

func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
    if let sampleBuffer = photoSampleBuffer,
      let previewBuffer = previewPhotoSampleBuffer,


      let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer){


      let dataProvider = CGDataProvider(data: dataImage as CFData)
      let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
      let imagea = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.right)

      UIImageWriteToSavedPhotosAlbum(imagea, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)



    }}



@IBAction func TakePhoto(_ sender: Any) {
    let settings = AVCapturePhotoSettings()
    let previewpixel = settings.availablePreviewPhotoPixelFormatTypes.first!
    let previewformat = [
      kCVPixelBufferPixelFormatTypeKey as String: previewpixel, kCVPixelBufferWidthKey as String: 160,
      kCVPixelBufferHeightKey as String : 160]

    settings.previewPhotoFormat = previewformat
    cameraOutput.capturePhoto(with: settings, delegate: self)
    }}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>更改下一行</p>

<pre><code>UIImage(cgImage: (otherImage?.cgImage!)!, scale: CGFloat(1.0), orientation: .right)
</code></pre>

<p>到</p>

<pre><code>UIImage(cgImage: (otherImage?.cgImage!)!, scale: CGFloat(1.0), orientation: .up)
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 保存旋转的图片(swift3),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/43298062/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/43298062/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 保存旋转的图片(swift3)