Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create ARReferenceImage programmatically

Tags:

ios

swift

arkit

I am downloading images from the database and want to use them as ARReferenceImage, instead of manually adding them to Xcode asset folder.

Code

    let image = #imageLiteral(resourceName: "mike")
    let cgImage = image.cgImage

    guard let referenceImages = ARReferenceImage.init(cgImage, orientation: .portrait, physicalWidth: 100) else {
        fatalError("Failed to load image")
    }

Error

Ambiguous reference to member 'init(_:orientation:physicalWidth:)'

like image 347
Mike Koene Avatar asked May 26 '26 00:05

Mike Koene


2 Answers

CGImagePropertyOrientation not support .portrait Please use below support:

public enum CGImagePropertyOrientation : UInt32 {


    case up // 0th row at top,    0th column on left   - default orientation

    case upMirrored // 0th row at top,    0th column on right  - horizontal flip

    case down // 0th row at bottom, 0th column on right  - 180 deg rotation

    case downMirrored // 0th row at bottom, 0th column on left   - vertical flip

    case leftMirrored // 0th row on left,   0th column at top

    case right // 0th row on right,  0th column at top    - 90 deg CW

    case rightMirrored // 0th row on right,  0th column on bottom

    case left // 0th row on left,   0th column at bottom - 90 deg CCW
}

Your code:

 let referenceImages = ARReferenceImage(cgImage!, orientation: CGImagePropertyOrientation.up, physicalWidth: 100) 

Or

let referenceImages = ARReferenceImage.init(cgImage!, orientation: CGImagePropertyOrientation.up, physicalWidth: 100)
like image 98
Jogendar Choudhary Avatar answered May 27 '26 14:05

Jogendar Choudhary


CGImagePropertyOrientation doesn't have a member named .portrait.

According to the documentation it has .up, .upMirrored, .down, .downMirrored, .leftMirrored, .right, .rightMirrored, .left. If you use one of these in your initializer it should work.

For example:

let referenceImage = ARReferenceImage(cgImage, orientation: .up, physicalWidth: 100)
like image 23
Paolo Avatar answered May 27 '26 16:05

Paolo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!