0

I use a swift native module in my react native ios app to generate an animated gif from assets url. It's working well at the first try but when I escape the component and pass new assets after it was used once, the app crash, sometimes displaying this error :

react error:

react error

or sometimes xcode redirects me to this objective-c function, telling me that the _instance to return have no value:

objective-c error

Here is how I call my native module in my react component:

callImages: function(){
var that = this
const imagesUri = this.parseImages();
var promise = new Promise(function(resolve, reject) {
  NativeModules.GifGenerator.initFunction(imagesUri, resolve)
});

promise.then(function(o) {
  base64 = "data:image/gif;base64,"+o.url
  imagesUri.push(base64);
  that.setState({images: imagesUri});
});
}

How I export my swift module in GifGenerator.m:

@interface RCT_EXTERN_MODULE(GifGenerator, NSObject)

RCT_EXTERN_METHOD(initFunction:(NSArray *)images 
                  callback:(RCTResponseSenderBlock)callback);

@end

And the importante part of my swift class :

func initFunction(images : [String], callback: (NSObject) -> () ){

self.standardImages = images

return getImages(images, handler: self.generationCallback , someCallback: callback )
}

...
(getImages call generate passing the callback function to it)
...

func generate(mainHandler: (NSObject) -> () ) -> Void {

...
(movie to gif generation using AVAssetWriter)
...

videoWriter.finishWritingWithCompletionHandler { () -> Void in
        if error == nil {

          let resultUrl = self.videoToGif(videoOutputURL)
          let ImageData = NSData(contentsOfURL: resultUrl)
          let imageBase64:String =         ImageData!.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
          mainHandler([["url": imageBase64], ["error": "non"]])

            if let data = NSData(contentsOfURL: resultUrl) {
              let image = UIImage(data: data)
              UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil);
            }

        }

They are the important part of my code but I can show you more if you need

1 Answers1

0

my guess is you need to reset your callback every time it is used on the swift side... So at the cop of your js callback method, re register your js cb to swift..

Chris Legge
  • 739
  • 2
  • 9
  • 24