Machineboy空

ARView 스냅샷, 캡처 함수 본문

언어/iOS

ARView 스냅샷, 캡처 함수

안녕도라 2024. 12. 17. 14:59

 

https://betterprogramming.pub/take-an-arview-snapshot-in-realitykit-93b620cf99b3

 

Take an ARView Snapshot in RealityKit

Build augmented reality iOS applications

betterprogramming.pub

https://developer.apple.com/documentation/realitykit/arview/snapshot(savetohdr:completion:)-66jzu

 

snapshot(saveToHDR:completion:) | Apple Developer Documentation

Takes a screenshot.

developer.apple.com

샘플코드

import SwiftUI
import RealityKit

struct ContentView : View {

    var body: some View {
        ZStack(alignment: .bottom){
          ARViewContainer().edgesIgnoringSafeArea(.all)
        Button {
          
            ARVariables.arView.snapshot(saveToHDR: false) { (image) in
              
              // Compress the image
              let compressedImage = UIImage(data: (image?.pngData())!)
              // Save in the photo album
              UIImageWriteToSavedPhotosAlbum(compressedImage!, nil, nil, nil)
            }
          
          } label: {
            Image(systemName: "camera")
              .frame(width:60, height:60)
              .font(.title)
              .background(.white.opacity(0.75))
              .cornerRadius(30)
              .padding()
          }
        }
    }

}

struct ARVariables{
  static var arView: ARView!
}

struct ARViewContainer:UIViewRepresentable {
    func makeUIView(context: Context) -> some UIView {
        ARVariables.arView = ARView(frame: .zero)
        
        let cube = ModelEntity(mesh: MeshResource.generateBox(size: 2), materials: [SimpleMaterial(color: .white, isMetallic: true)])
        
        let anchor = AnchorEntity(plane: .horizontal)
        anchor.addChild(cube)
        
        ARVariables.arView.scene.addAnchor(anchor)
        
        return ARVariables.arView
    }
    func updateUIView(_ uiView: UIViewType, context: Context) {
      
    }
}

#Preview {
    ContentView()
}

p.Infolist

  • 카메라 허용은 AR프리셋으로 프로젝트를 시작하면 자동으로 설정되어 있음.
  • Privacy- Photo Library Usage Description 설정