ios - 尝试从 objects[0] 中插入 nil 对象
问题描述
我的 React Native 应用程序的一个功能包括对文档照片进行透视校正。
它需要 4 个点,裁剪图像,透视校正,然后应用 CIFilter 调整颜色并导出为 Base64 字符串。
我们试图在 iPhone 11 模拟器上运行它
截至目前,我们一直收到此错误
attempt to insert nil object from objects[0]
假设:这可能是因为它无法从文件存储中读取图像/文件被读取为nil
这是源代码
#import "CustomCropManager.h"
#import <React/RCTLog.h>
@implementation CustomCropManager
RCT_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
解决方案
“必须有 50 声望才能发表评论”
您应该输出您尝试使用的每个变量。更改
NSLog(@"[myLOG] ciImage");
为NSLog(@"[myLOG] ciImage %@", ciImage);
如果它打印出来,NSLog(@"[myLOG] ciImage");
那么崩溃必须在那之后和下一次之前发生NSLog
。意味着这条线失败:
[colorControlsFilter setValue:ciImage forKey:@"inputImage"];
正如您所说,图像nil
意味着设置图像的行包含错误。
ciImage = [ciImage imageByApplyingFilter:@"CIPerspectiveCorrection" withInputParameters:rectangleCoordinates];
过滤器是否存在并且是否正确编写?
出于调试目的,您可以取消注释此行并查看是否显示原始图像。
推荐阅读
- java - Java 模拟类方法和注入服务
- angular - 索赔过滤器,带角度和弹簧护套
- scala - Gatling:如何从 JSON 馈送文件加载范围?
- python - seaborn.catplot 在更改 x 和 y 的值时出现 ValueError
- python - 在 Heroku 上找不到 torch==1.5.0+cpu 的匹配分布
- java - 虽然循环不适用于名称长度验证(java)
- python - Python读取带有汉字的xml文件
- ios - 在其他函数中访问 tableview 的属性
- angular - Angular ReactiveFromm:setValue 是否应该触发 ngModelChange
- java - Mybatis中Oracle绑定变量