0

我不确定我在这里做错了什么,但是当我运行我的应用程序以在 Appcelerator Hyperloop iOS 中对图像进行灰度化时,出现运行时错误。错误是在尝试运行函数 CGBitmapContextCreate 时。任何帮助,将不胜感激。

代码在这里:

  var Foundation = require('Foundation');
  var NSFileManager = require('Foundation/NSFileManager');
  var NSMutableArray = require('Foundation/NSMutableArray');
  var NSArray = require('Foundation/NSArray');
  var NSData = require('Foundation/NSData');
  var NSURL = require('Foundation/NSURL');
  var NSString = require('Foundation/NSString');
  var UIKit = require('UIKit');
  var UIImage = require('UIKit/UIImage');
  var CoreGraphics = require('CoreGraphics');

  var cachesDir = Foundation.NSHomeDirectory() + "/Library/Caches/";

  var imgPath = cachesDir + filename;
  Ti.API.info("grayscaleIOSImage imgPath " + imgPath);

  var fileManager = NSFileManager.alloc().init().autorelease();
  Ti.API.info("grayscaleIOSImage fileExistsAtPath " + fileManager.fileExistsAtPath(imgPath));

  var imgData = NSData.alloc().initWithContentsOfURL(NSURL.fileURLWithPathIsDirectory(imgPath, false));
  var uiimage = UIImage.alloc().initWithData(imgData);

  // Create image rectangle with current image width/height
  var imageRect = CoreGraphics.CGRectMake(0, 0, uiimage.size.width, uiimage.size.height);

  // Grayscale color space
  Ti.API.info("grayscaleIOSImage CoreGraphics.CGBitmapContextCreate " + CoreGraphics.CGBitmapContextCreate);

  // Create bitmap content with current image size and grayscale colorspace
  var context = CoreGraphics.CGBitmapContextCreate(null, uiimage.size.width, uiimage.size.height, 8, 0, CoreGraphics.CGColorSpaceCreateDeviceGray(), CoreGraphics.kCGImageAlphaNone);

...

日志结果在这里:

[ERROR] -[NSNull pointerValue]: unrecognized selector sent to instance 0x11608aef0
[ERROR] Script Error {
[ERROR]     column = 26;
[ERROR]     description = "-[NSNull pointerValue]: unrecognized selector sent to instance 0x11608aef0";
[ERROR]     line = 41;
[ERROR]     message = "-[NSNull pointerValue]: unrecognized selector sent to instance 0x11608aef0";
[ERROR]     name = NSInvalidArgumentException;
[ERROR]     nativeStack = "1   libobjc.A.dylib                     0x0000000114d67f41 objc_exception_throw + 48\n2   CoreFoundation                      0x0000000115e42024 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132\n3   CoreFoundation                      0x0000000115d43f78 ___forwarding___ + 1432\n4   CoreFoundation                      0x0000000115d43958 _CF_forwarding_prep_0 + 120\n5   Facebank                            0x000000010d61520b +[HyperloopCoreGraphics CGBitmapContextCreate:] + 315\n6   CoreFoundation                      0x0000000115d4536c __invoking___ + 140\n7   CoreFoundation                      0x0000000115d45240 -[NSInvocation invoke] + 320\n8   Facebank                            0x000000010d64853e +[HyperloopUtils invokeSelector:args:target:instance:] + 1550\n9   Facebank                            0x000000010d63d1f1 Dispatch + 1489\n10  JavaScriptCore                      0x000000010dea33d5 _ZN3JSC19APICallbackFunction4callINS_18JSCallbackFunctionEEExPNS_9ExecStateE + 501\n11  JavaScriptCore                      0x000000010dfb2cd1 _ZN3JSC5LLInt9setUpCallEPNS_9ExecStateEPNS_11InstructionENS_22CodeSpecializationKindENS_7JSValueEPNS_17LLIntCallLinkInfoE + 577\n12  JavaScriptCore                      0x000000010dfbbf4d llint_entry + 27269\n13  JavaScriptCore                      0x000000010dfbbf5b llint_entry + 27283\n14  JavaScriptCore                      0x000000010dfbbf5b llint_entry + 27283\n15  JavaScriptCore                      0x000000010dfbc296 llint_entry + 28110\n16  JavaScriptCore                      0x000000010dfbbf5b llint_entry + 27283\n17  JavaScriptCore                      0x000000010dfbbf5b llint_entry + 27283\n18  JavaScriptCore                      0x000000010dfb52e0 vmEntryToJavaScript + 304\n19  JavaScriptCore                      0x000000010de461df _ZN3JSC7JITCode7executeEPNS_2VMEPNS_14ProtoCallFrameE + 127\n20  JavaScriptCore                      0x000000010de0c4a3 _ZN3JSC11Interpreter14executeProgramERKNS_10SourceCodeEPNS_9ExecStateEPNS_8JSObjectE + 13075\n21  JavaScriptCore                      0x000000010db59c6f _ZN3JSC8evaluateEPNS_9ExecStateERKNS_10SourceCodeENS_7JSValueERN3WTF8NakedPtrINS_9ExceptionEEE + 303\n22  JavaScriptCore                      0x000000010dea14c0 JSEvaluateScript + 432\n23  Facebank                            0x000000010d34ad83 -[KrollEval jsInvokeInContext:exception:] + 259\n24  Facebank                            0x000000010d3467cb -[KrollBridge loadCommonJSModule:withSourceURL:] + 395\n25  Facebank                            0x000000010d3476c0 -[KrollBridge loadJavascriptText:fromFile:withContext:] + 96\n26  Facebank                            0x000000010d347948 -[KrollBridge cachedLoadAsFile:asJSON:withContext:] + 264\n27  Facebank                            0x000000010d347a77 -[KrollBridge loadAsFile:withContext:] + 263\n28  Facebank                            0x000000010d347da1 -[KrollBridge loadAsFileOrDirectory:withContext:] + 81\n29  Facebank                            0x000000010d348703 -[KrollBridge require:path:] + 947\n30  Facebank                            0x000000010d34c6fb CommonJSRequireCallback + 187\n31  JavaScriptCore                      0x000000010dea33d5 _ZN3JSC19APICallbackFunction4callINS_18JSCallbackFunctionEEExPNS_9ExecStateE + 501\n32  JavaScriptCore                      0x000000010dfb2cd1 _ZN3JSC5LLInt9setUpCallEPNS_9ExecStateEPNS_11InstructionENS_22CodeSpecializationKindENS_7JSValueEPNS_17LLIntCallLinkInfoE + 577\n33  JavaScriptCore                      0x000000010dfbbf4d llint_entry + 27269\n34  JavaScriptCore                      0x000000010dfbbf5b llint_entry + 27283\n35  JavaScriptCore                      0x000000010dfbbf5b llint_entry + 27283\n36  JavaScriptCore                      0x000000010dfb52e0 vmEntryToJavaScript + 304\n37  JavaScriptCore                      0x000000010de461df _ZN3JSC7JITCode7executeEPNS_2VMEPNS_14ProtoCallFrameE + 127\n38  JavaScriptCore                      0x000000010de0c4a3 _ZN3JSC11Interpreter14executeProgramERKNS_10SourceCodeEPNS_9ExecStateEPNS_8JSObjectE + 13075\n39  JavaScriptCore                      0x000000010db59c6f _ZN3JSC8evaluateEPNS_9ExecStateERKNS_10SourceCodeENS_7JSValueERN3WTF8NakedPtrINS_9ExceptionEEE + 303\n40  JavaScriptCore                      0x000000010dea14c0 JSEvaluateScript + 432\n41  Facebank                            0x000000010d344c11 -[KrollBridge evalFileOnThread:context:] + 1281\n42  Facebank                            0x000000010d34a77c -[KrollInvocation invoke:] + 124\n43  Facebank                            0x000000010d34b9df -[KrollContext invoke:] + 159\n44  Facebank                            0x000000010d34bd75 -[KrollContext invokeOnThread:method:withObject:callback:selector:] + 165\n45  Facebank                            0x000000010d344d83 -[KrollBridge evalFile:callback:selector:] + 115\n46  Facebank                            0x000000010d345d42 -[KrollBridge didStartNewContext:] + 2898\n47  Facebank                            0x000000010d34c621 -[KrollContext main] + 1761\n48  Facebank                            0x000000010d34b7b6 __21-[KrollContext start]_block_invoke + 38\n49  libdispatch.dylib                   0x0000000116ffa177 _dispatch_call_block_and_release + 12\n50  libdispatch.dylib                   0x0000000116ffb1ba _dispatch_client_callout + 8\n51  libdispatch.dylib                   0x00000001170053a4 _dispatch_main_queue_callback_4CF + 1260\n52  CoreFoundation                      0x0000000115d83e39 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9\n53  CoreFoundation                      0x0000000115d48462 __CFRunLoopRun + 2402\n54  CoreFoundation                      0x0000000115d47889 CFRunLoopRunSpecific + 409\n55  GraphicsServices                    0x0000000118be29c6 GSEventRunModal + 62\n56  UIKit                               0x000000010fa5c5d6 UIApplicationMain + 159\n57  Facebank                            0x000000010d332181 main + 113\n58  libdyld.dylib                       0x000000011706fd81 start + 1";
[ERROR]     sourceURL = "file:///Users/christopherhong/Library/Developer/CoreSimulator/Devices/2688670C-67BF-40B8-A8EC-3EE8669FFFC4/data/Containers/Bundle/Application/6C204BAF-D7E3-4F1F-BC65-4D12A2B55661/Facebank.app/hyperloop/coregraphics/coregraphics.js";
[ERROR]     stack = "    at dispatch@[native code]\n    at value(/hyperloop/coregraphics/coregraphics.js:41:26)\n    at grayscaleIOSImage(/alloy/controllers/index.js:237:88)\n    at Controller(/alloy/controllers/index.js:287:20)\n    at createController(/alloy.js:339:53)\n    at (/app.js:39:23)\n    at global code(/app.js:41:70)\n    at require@[native code]\n    at (/ti.main.js:28:10)\n    at loadAsync(/ti.internal/bootstrap.loader.js:102:13)\n    at global code(/ti.main.js:25:52)";
[ERROR]     toJSON = "<KrollCallback: 0x6040002758c0>";
[ERROR] } 
[ERROR] Script Error Module "app.js" failed to leave a valid exports object
4

1 回答 1

0

结果是 Objective-C 中的 CGBitmapContextCreate 在其第一个参数中需要一个 nil 值:

CoreGraphics.CGBitmapContextCreate(nil, uiimage.size.width, uiimage.size.height, 8, 0, CoreGraphics.CGColorSpaceCreateDeviceGray(), CoreGraphics.kCGImageAlphaNone);

Javascript 中没有 nil 之类的东西:

CoreGraphics.CGBitmapContextCreate(null, uiimage.size.width, uiimage.size.height, 8, 0, CoreGraphics.CGColorSpaceCreateDeviceGray(), CoreGraphics.kCGImageAlphaNone);

在 CGBitmapContextCreate Objective-C 函数调用中,pointerValue 选择器在 nil 对象上运行。该选择器调用在 nil 对象上的本机 Objective-C 中运行得非常好。当在作为 NSNull 对象发送到 Objective-C 的 Hyperloop Javascript null 对象上运行时,该选择器调用将崩溃。

"-[NSNull pointerValue]: unrecognized selector sent to instance 0x11608aef0";

Nil vs NSNull

于 2019-03-20T19:11:46.670 回答