0

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

以下是显示正在发生的事情的图像 1. 在应用程序中拍摄的照片,2. 在 Instagram 中水平放置的照片 在应用程序中拍摄的照片 照片发布到 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,但是当我在相机上拍照时,它会水平翻转它。

知道为什么会这样吗?

4

1 回答 1

1

听到您正在使用 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
于 2022-01-22T08:40:23.863 回答