从我的应用分享到 Instagram Story 时,Swiftui IGStoryKit 垂直图像会水平翻转

Swiftui IGStoryKit Vertical Image flips Horizontal when sharing to Instagram Story from my app

我的应用程序中有一个功能可以拍照,然后将该照片分享到您的 Instagram 快拍。当用户拍照时,它是垂直的,并在我的应用程序中占据了全屏。然后,当用户点击将照片分享到他们的 Instagram 故事时,它会将照片水平翻转。

这些图片显示了正在发生的事情 1. 在应用程序中拍摄的照片,2. 在 Instagram 中水平放置的照片

用户从 'CameraView' 开始,代码如下:

struct CameraView: View {

@StateObject var camera = CameraModel()

var body: some View {
    
    
    ZStack {
        
        //GOING TO BE CAMERA PREVIEW
        CameraPreview(camera: camera)
            .ignoresSafeArea(.all, edges: .all)
        
        VStack {
            
            if camera.isTaken {
                
                HStack {
                    
                    Spacer()
                    
                    Button(action: {
                        camera.reTake()
                    }, label: {
                        Image(systemName: "arrow.triangle.2.circlepath.camera")
                            .foregroundColor(.black)
                            .padding()
                            .background(Color.white)
                            .clipShape(Circle())
                    })
                    .padding(.trailing, 10)
                }
            }
            
            Spacer()
            
            HStack {
                
                if camera.isTaken {
                    
                    Spacer()
                    
                    Button(action: {
                        camera.sharePic()
                    }, label: {
                        Text("Share")
                            .foregroundColor(.black)
                            .fontWeight(.semibold)
                            .padding(.vertical, 10)
                            .padding(.horizontal, 20)
                            .background(Color.white)
                            .clipShape(Capsule())
                    })
                    .padding(.trailing)
                    
                } else {
                    Button(action: {
                        camera.takePic()
                    }, label: {
                        ZStack {
                            Circle()
                                .fill(Color.white)
                                .frame(width: 65, height: 65)
                            
                            Circle()
                                .stroke(Color.white, lineWidth: 2)
                                .frame(width: 75, height: 75)
                        }
                    })
                }
            }
            .frame(height: 75)
            
        }
        
    }
    .onAppear(perform: {
        camera.check()
    })
    
    
//END OF VAR BODY AND STRUCT
}

这是 'CameraPreview' 的代码,它显示相机或用户拍摄的照片:

//CAMERA PREVIEW
struct CameraPreview: UIViewRepresentable {
@ObservedObject var camera : CameraModel

func makeUIView(context: Context) -> UIView {
    
    let view = UIView(frame: UIScreen.main.bounds)
    
    camera.preview = AVCaptureVideoPreviewLayer(session: camera.session)
    camera.preview.frame = view.frame
    
    //YOUR OWN PROPERTIES
    camera.preview.videoGravity = .resizeAspectFill
    view.layer.addSublayer(camera.preview)
    
    //STARTING SESSION
    camera.session.startRunning()
    
    return view
}

func updateUIView(_ uiView: UIView, context: Context) {
    
}

这是 'CameraModel' 我使用的所有相机功能

//CAMERA MODEL
class CameraModel: NSObject, ObservableObject, AVCapturePhotoCaptureDelegate {
@Published var isTaken = false
@Published var session = AVCaptureSession()
@Published var alert = false
@Published var output = AVCapturePhotoOutput()
//PREVIEW
@Published var preview : AVCaptureVideoPreviewLayer!

@Published var picData = Data(count: 0)

func check() {
    //FIRST CHECKING CAMERA HAS GOT PERMISSION
    switch AVCaptureDevice.authorizationStatus(for: .video) {
    case .authorized:
        setUp()
        return
        //SETTING UP SESSSION
    case .notDetermined:
        //REASKING FOR PERMISSION
        AVCaptureDevice.requestAccess(for: .video) { (status) in
            if status {
                self.setUp()
            }
        }
    case .denied:
        self.alert.toggle()
        return
    default:
        return
    }
}

func setUp() {
    
    //SETTING UP CAMERA
    do{
        //SETTING CONFIGS
        self.session.beginConfiguration()
        
        //CHANGE FOR YOUR OWN
        let device = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .back)
        
        let input = try AVCaptureDeviceInput(device: device!)
        
        //CHECKING AND ADDING TO SESSION
        if self.session.canAddInput(input){
            self.session.addInput(input)
        }
        
        //SAME FOR OUTPUT
        if self.session.canAddOutput(self.output){
            self.session.addOutput(self.output)
        }
        
        self.session.commitConfiguration()
    }
    catch{
        print(error.localizedDescription)
    }
}

//TAKE PIC AND RETAKE PIC
func takePic() {
    DispatchQueue.global(qos: .background).async {
        self.output.capturePhoto(with: AVCapturePhotoSettings(), delegate: self)
        self.session.stopRunning()
        
        DispatchQueue.main.async {
            withAnimation{self.isTaken.toggle()}
        }
    }
}

func reTake(){
    DispatchQueue.global(qos: .background).async {
        self.session.startRunning()
        DispatchQueue.main.async {
            withAnimation{self.isTaken.toggle()}
        }
    }
}

func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    if error != nil {
        return
    }
    
    print("DEBUG: Picture Taken")
    
    guard let imageData = photo.fileDataRepresentation() else { return }
    
    self.picData = imageData
}

func sharePic() {
    //VARIABLE FOR THE IMAGE TAKEN
    let image = UIImage(data: self.picData)!
    
    //IG STORY PACKAGE VARIABLE
    let igData = IGData(backgroundType: .image, colorTop: .systemOrange, colorBottom: .systemRed, backgroundImage: image.withTintColor(.red), contentSticker: UIImage(systemName: "person.fill.checkmark"))
    
    //SHARING THE IMAGE TO INSTAGRAM STORY
    let igDispatcher = IGDispatcher(igData: igData)
    igDispatcher.start()
    
}

我正在使用 swift 包“IGStoryKit”来处理拍摄用户拍摄的照片并将其发布到他们的 Instagram 故事中。当我在我的应用程序资产中使用普通的垂直照片时,它会以垂直风景正确地发布到 Instagram,但是当我在相机上拍摄照片时,它会水平翻转。

知道为什么会这样吗?

听说你在使用 IGStoryKit 超级兴奋(我做到了!)。

我也遇到过这个问题。在将图像传递到 IGStoryKit 之前,您必须旋转图像。这是一个 iOS“问题”

我在我的一个应用程序中使用了这段代码。请尝试一下,让我知道它是否也适合您!

var fixOrientation: UIImage? {
        guard self.imageOrientation != .up else { return self }
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        let rect = CGRect(x: 0,
                          y: 0,
                          width: self.size.width,
                          height: self.size.height)
        self.draw(in: rect)
        guard let normalizedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() else { return nil }
        UIGraphicsEndImageContext()
        return normalizedImage
}

您必须将此应用到图像上。所以,在你的情况下是这样的:

let image = UIImage(data: self.picData)!
guard let fixedOrientationImage = image.fixOrientation else { return }

// Now pass this `fixedOrientationImage` to IGDispatcher