首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Appcelerator Hyperloop CoreGraphics CGBitmapContextCreate无法运行

Appcelerator Hyperloop CoreGraphics CGBitmapContextCreate无法运行
EN

Stack Overflow用户
提问于 2019-02-25 08:33:51
回答 1查看 81关注 0票数 0

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

代码如下:

代码语言:javascript
复制
  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);

..。

日志结果如下:

代码语言:javascript
复制
[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
EN

回答 1

Stack Overflow用户

发布于 2019-03-21 03:11:47

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

代码语言:javascript
复制
CoreGraphics.CGBitmapContextCreate(nil, uiimage.size.width, uiimage.size.height, 8, 0, CoreGraphics.CGColorSpaceCreateDeviceGray(), CoreGraphics.kCGImageAlphaNone);

Javascript中没有nil这样的东西:

代码语言:javascript
复制
CoreGraphics.CGBitmapContextCreate(null, uiimage.size.width, uiimage.size.height, 8, 0, CoreGraphics.CGColorSpaceCreateDeviceGray(), CoreGraphics.kCGImageAlphaNone);

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

代码语言:javascript
复制
"-[NSNull pointerValue]: unrecognized selector sent to instance 0x11608aef0";

Nil vs NSNull

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54858021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档