1

I've created AR app which detects image, upon image detection I want to play gif on top of it.

I followed this tutorial to detect image: https://www.raywenderlich.com/6957-building-a-museum-app-with-arkit-2

In VC I added Imageview like this:

  var imageView = GIFImageView(frame: CGRect(x: 0, y: 0, width: 600, height: 600))

Here is my code in ARSCNViewDelegate didAdd node for method.

  func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
DispatchQueue.main.async { self.instructionLabel.isHidden = true }
if let imageAnchor = anchor as? ARImageAnchor {
  //      handleFoundImage(imageAnchor, node)

  let size = imageAnchor.referenceImage.physicalSize

  DispatchQueue.main.async(){ // If we remove this we are getting UIview setAnimation is being call from background thread error is coming.
    self.imageView.animate(withGIFNamed: "tenor.gif") // I actually access gif from Document folder i.e Data format
  }
  let imgMaterial = SCNMaterial()

  imgMaterial.diffuse.contents = imageView

  let imgPlane = SCNPlane(width: size.width, height: size.height)

  imgPlane.materials = [imgMaterial]

  let imgNode = SCNNode(geometry: imgPlane)
  imgNode.eulerAngles.x = -.pi / 2

  node.addChildNode(imgNode)
  node.opacity = 1

}

}

After playing gif when I go back to my previous/next/same VC I can't tap on any UI elements(buttons etc).

In console I see this but I did not find solution to this. view animation is there in UIImage+gif swift file.

UIView setAnimationsEnabled being called from a background thread. Performing any operation from a background thread on UIView or a subclass is not supported and may result in unexpected and insidious behavior

Just run this in Device. https://drive.google.com/file/d/1FKHPO6SkdOEZ-w_GFnrU5CeeeMQrNT-h/view?usp=sharing

You just run this project in device and scan dinosaur.png image(added ion xcode) you will gif playing on top of it. Once if you go back to firstVC that's all app is freezed you can't tap on any button in First VC and also hyou can't start AR scene again. I can't figure out this issue why it's happening after palying GIF can you pleach check and let me know.

If anything is required please let me know.. Thanks in advance.

1 Answers1

0

Gif are a bit annoying to handle is iOS, and because of that, I use mp4 resources instead of Gif, which can be loaded into a AVPlayer directly.

In your case I see that you have the resources in the Bundle, so you can convert them to mp4 and use it instead.

To play a video in SceneKit you can use this link How do I create a looping video material in SceneKit on iOS in Swift 3?.

Mihai Georgescu
  • 674
  • 8
  • 20
  • Hi, In this solution they did not mentioned playing transparent background video, so Instead I planned to play gif without BG. Everything works fine in current implementation until GIF is playing, Once if I came out of GIF play app UI Got freeze and in console it shows-> UIView setAnimationsEnabled being called from a background thread. I didn't find it's solution even I added DispatchQueue.main.async{} but not working. –  Mar 02 '20 at 11:43
  • HI Bro, GIF I'm able to play properly with GIFU library but in my console see this issue due to that app ui freezing. Can you help me fixing this issue. **UIView setAnimationsEnabled being called from a background thread. Performing any operation from a background thread on UIView or a subclass is not supported and may result in unexpected and insidious behavior** –  Mar 03 '20 at 13:57
  • First check exactly from where the issue is coming from, enable Main Thread Checker on your schema, run it with the debugger attached and see what triggers it. I had a similar issue with SceneKit at first load the Main Thread Checker was detecting an issue, but in my case it was an apple bug which was only reproducing in debug not also in release mode. – Mihai Georgescu Mar 03 '20 at 14:57
  • Bro Main thread checker is enabled Schema, (run it with the debugger attached and see what triggers it.)-> means what bro I'm running app in iPad device it gives the same issue in console. Actually I did not used any threading to play gif but it shows this issue in SceneKit. I'm using Xcode 11.3 is that a apple bug?? not able to figure out where UIView setAnimation is called on Background thread. –  Mar 03 '20 at 15:24
  • In XCode11, Tap the arrow near Main Thread Checker and it will add a Breakpoint that will get trigger when ever the checker is triggered. Check the call stack to see from where it comes, here is a bit more info on how to debug with Main Thread Checker https://stackoverflow.com/questions/44943995/what-is-main-thread-checker-in-xcode – Mihai Georgescu Mar 03 '20 at 15:28
  • I tried it in Xcode 11.3 not getting any runtime main thread issues please have a look I added my app source code link my question. Still app freeze issue is there please check. Thank you. –  Mar 03 '20 at 18:42
  • HI, The only thing I got in my Xcode 11.3 at runtime is this:: **GPU Frame Capture: Shader performance data maybe unavailable due to deployment target older than device version.** In my app consoe it prints as **UIView setAnimationsEnabled being called from a background thread. Performing any operation from a background thread on UIView or a subclass is not supported and may result in unexpected and insidious behavior** after that my app UI is freeze. Main thread checker is not going to the place where issue is happening. Pls any suggestion?? –  Mar 04 '20 at 07:03
  • I could not reproduce your issue, your sample project is not just plug and play, you have to scan some object and put it in the asset bundle, and in the end in the renderer callback you are checking for `if let imageAnchor = anchor as? ARImageAnchor {` which won't pass, because it is an object. Can't help you anymore. – Mihai Georgescu Mar 04 '20 at 11:12
  • HI Did you find the sollution for this issue – Tommy Oct 19 '20 at 19:06