A feature of my React Native App consists of making Perspective Corrections to Document Photos.
It takes 4 points, crops the image, perspective corrects it, and then applys CIFilter to adjust the colours and export as Base64 String.
We were trying to run this on an iPhone 11 Simulator
As of now, we have been getting this error
attempt to insert nil object from objects[0]
Assumption: It is probably happening because it can't read the image from file storage / the file is being read as nil
Here is the source code
#import "CustomCropManager.h"#import <React/RCTLog.h>@implementation CustomCropManagerRCT_EXPORT_MODULE();RCT_EXPORT_METHOD(crop:(NSDictionary *)points imageUri:(NSString *)imageUri callback:(RCTResponseSenderBlock)callback){ NSLog(@"[myLOG] PARSING"); NSString *parsedImageUri = [imageUri stringByReplacingOccurrencesOfString:@"file://" withString:@""]; NSLog(@"[myLOG] parsedImageUri"); NSURL *fileURL = [NSURL fileURLWithPath:parsedImageUri]; NSLog(@"[myLOG] fileURL"); CIImage *ciImage = [CIImage imageWithContentsOfURL:fileURL]; NSLog(@"[myLOG] ciImage"); CGPoint newLeft = CGPointMake([points[@"topLeft"][@"x"] floatValue], [points[@"topLeft"][@"y"] floatValue]); CGPoint newRight = CGPointMake([points[@"topRight"][@"x"] floatValue], [points[@"topRight"][@"y"] floatValue]); CGPoint newBottomLeft = CGPointMake([points[@"bottomLeft"][@"x"] floatValue], [points[@"bottomLeft"][@"y"] floatValue]); CGPoint newBottomRight = CGPointMake([points[@"bottomRight"][@"x"] floatValue], [points[@"bottomRight"][@"y"] floatValue]); NSLog(@"[myLOG] CGPOINTS"); newLeft = [self cartesianForPoint:newLeft height:[points[@"height"] floatValue] ]; newRight = [self cartesianForPoint:newRight height:[points[@"height"] floatValue] ]; newBottomLeft = [self cartesianForPoint:newBottomLeft height:[points[@"height"] floatValue] ]; newBottomRight = [self cartesianForPoint:newBottomRight height:[points[@"height"] floatValue] ]; NSLog(@"[myLOG] new"); NSMutableDictionary *rectangleCoordinates = [[NSMutableDictionary alloc] init]; rectangleCoordinates[@"inputTopLeft"] = [CIVector vectorWithCGPoint:newLeft]; rectangleCoordinates[@"inputTopRight"] = [CIVector vectorWithCGPoint:newRight]; rectangleCoordinates[@"inputBottomLeft"] = [CIVector vectorWithCGPoint:newBottomLeft]; rectangleCoordinates[@"inputBottomRight"] = [CIVector vectorWithCGPoint:newBottomRight]; NSLog(@"[myLOG] rectangleCoordinates"); ciImage = [ciImage imageByApplyingFilter:@"CIPerspectiveCorrection" withInputParameters:rectangleCoordinates]; NSLog(@"[myLOG] ciImage"); // custom code CIFilter* colorControlsFilter = [CIFilter filterWithName:@"CIColorControls"]; [colorControlsFilter setValue:ciImage forKey:@"inputImage"]; [colorControlsFilter setValue:[NSNumber numberWithFloat:1.0] forKey:@"inputSaturation"];[colorControlsFilter setValue:[NSNumber numberWithFloat:0.2] forKey:@"inputBrightness"];[colorControlsFilter setValue:[NSNumber numberWithFloat:1.0] forKey:@"inputContrast"]; ciImage = [colorControlsFilter valueForKey:@"outputImage"]; // custom code NSLog(@"[myLOG] ciImage ssss"); CIContext *context = [CIContext contextWithOptions:nil]; CGImageRef cgimage = [context createCGImage:ciImage fromRect:[ciImage extent]]; UIImage *image = [UIImage imageWithCGImage:cgimage]; NSLog(@"[myLOG] image"); NSData *imageToEncode = UIImageJPEGRepresentation(image, 0.8); NSLog(@"[myLOG] calling..."); callback(@[[NSNull null], @{@"image": [imageToEncode base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]}]);}- (CGPoint)cartesianForPoint:(CGPoint)point height:(float)height { return CGPointMake(point.x, height - point.y);}@end