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:
or sometimes xcode redirects me to this objective-c function, telling me that the _instance to return have no value:
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