【问题标题】:iOS 8.1 with ZXING error ARC forbids explicit message send of 'autorelease'带有 ZXING 错误 ARC 的 iOS 8.1 禁止发送“自动释放”的显式消息
【发布时间】:2014-12-15 10:43:19
【问题描述】:

我使用 ZXING 为 Android 开发了一个 DataMatrix Reader,并且工作正常,现在我正在使用 iOS 版本,但是当我想在我的项目中使用该库时出现此错误:

iOS SDK 8.1 和库 ZXING:https://github.com/TheLevelUp/ZXingObjC

我用的是 COCOAPODS:

平台:ios,'7.0' pod 'ZXingObjC', '~> 3.0'

我在我的项目中使用 Cocoapods 实现了这个库,现在我想在我的应用程序中这样使用:

#import "ViewController.h"
#import "ZXingObjC.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)scanBarcode:(id)sender {

    CGImageRef imageToDecode;  // Given a CGImage in which we are looking for barcodes

    ZXLuminanceSource *source = [[[ZXCGImageLuminanceSource alloc] initWithCGImage:imageToDecode] autorelease];
    ZXBinaryBitmap *bitmap = [ZXBinaryBitmap binaryBitmapWithBinarizer:[ZXHybridBinarizer binarizerWithSource:source]];

    NSError *error = nil;

    // There are a number of hints we can give to the reader, including
    // possible formats, allowed lengths, and the string encoding.
    ZXDecodeHints *hints = [ZXDecodeHints hints];

    ZXMultiFormatReader *reader = [ZXMultiFormatReader reader];
    ZXResult *result = [reader decode:bitmap
                                hints:hints
                                error:&error];
    if (result) {
        // The coded result as a string. The raw data can be accessed with
        // result.rawBytes and result.length.
        NSString *contents = result.text;

        // The barcode format, such as a QR code or UPC-A
        ZXBarcodeFormat format = result.barcodeFormat;
    } else {
        // Use error to determine why we didn't get a result, such as a barcode
        // not being found, an invalid checksum, or a format inconsistency.
    }
}
@end

但我有这个错误:

DataMatrixReader/ViewController.m:32:99:ARC 禁止显式消息 发送“自动释放”

DataMatrixReader/ViewController.m:32:99:“自动释放”不可用: 在自动引用计数模式下不可用

具体在这一行:

ZXLuminanceSource *source = [[[ZXCGImageLuminanceSource alloc] initWithCGImage:imageToDecode] autorelease];

来自 "iamnichols" 的帮助...在换行之后:

ZXLuminanceSource *source = [[[ZXCGImageLuminanceSource alloc] initWithCGImage:imageToDecode] autorelease];

ZXLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:imageToDecode];

错误:

:CGBitmapContextGetData:无效的上下文 0x0。这是一个 严重错误。此应用程序或它使用的库正在使用 无效的上下文,从而导致整体退化 系统的稳定性和可靠性。此通知是一种礼貌:请 解决这个问题。这将成为即将到来的更新中的致命错误。 2014-12-15 12:12:36.122 DataMatrixReader[18838:412778] * 终止 应用程序由于未捕获的异常“NSInvalidArgumentException”,原因: '两个维度都必须大于 0' * 第一次抛出调用堆栈:( 0 CoreFoundation 0x0000000102626f35 exceptionPreprocess + 165 1 libobjc.A.dylib
0x00000001022bfbb7 objc_exception_throw + 45 2 DataMatrixReader
0x0000000100f09507 -[ZXBitMatrix initWithWidth:height:] + 231 3
DataMatrixReader 0x0000000100f51b7d -[ZXGlobalHistogramBinarizer blackMatrixWithError:] + 141 4 DataMatrixReader 0x0000000100f530b0 -[ZXHybridBinarizer blackMatrixWithError:] + 816 5 DataMatrixReader 0x0000000100f068ab -[ZXBinaryBitmap blackMatrixWithError:] + 139 6
DataMatrixReader 0x0000000100fa43b2 -[ZXQRCodeReader decode:hints:error:] + 130 7 DataMatrixReader 0x0000000100f63d04 -[ZXMultiFormatReader decodeInternal:error:] + 548 8 数据矩阵读取器 0x0000000100f62ade -[ZXMultiFormatReader decode:hints:error:] + 142 9 DataMatrixReader 0x0000000100ef1df2 -[ViewController scanBarcode:] + 322 10 UIKit
0x0000000102a148be -[UIApplication sendAction:to:from:forEvent:] + 75 11 UIKit 0x0000000102b1b410 -[UIControl _sendActionsForEvents:withEvent:] + 467 12 UIKit 0x0000000102b1a7df -[UIControl touchesEnded:withEvent:] + 522 13 UIKit 0x0000000102a5a308 -[UIWindow _sendTouchesForEvent:] + 735 14 UIKit 0x0000000102a5ac33 -[UIWindow sendEvent:] + 683 15 UIKit
0x0000000102a279b1 -[UIApplication sendEvent:] + 246 16 UIKit
0x0000000102a34a7d _UIApplicationHandleEventFromQueueEvent + 17370 17 UIKit 0x0000000102a10103 _UIApplicationHandleEventQueue + 1961 18 核心基础 0x000000010255c551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION
+ 17 19 核心基础 0x000000010255241d __CFRunLoopDoSources0 + 269 20 CoreFoundation 0x0000000102551a54 __CFRunLoopRun + 868 21 CoreFoundation
0x0000000102551486 CFRunLoopRunSpecific + 470 22 图形服务
0x0000000104bc09f0 GSEventRunModal + 161 23 UIKit
0x0000000102a13420 UIApplicationMain + 1282 24 DataMatrixReader
0x0000000100ef2343 main + 115 25 libdyld.dylib
0x00000001058a3145 start + 1 ) libc++abi.dylib: 终止于 NSException (lldb) 类型的未捕获异常

现在的问题是有人试图将 ZxingObjC 集成到 iOS 8.1 中?

【问题讨论】:

  • 你真的看过错误信息吗?
  • 是的,但是现在我有另一个之后来实现该行以避免最后一个错误。
  • 第二个错误可能是因为imageToDecodeNULL。这段代码是您自己编写的,还是您只是从互联网上复制下来并希望它能够正常工作?
  • SO 不是调试服务。如果您不知道自己在这里做什么,也许您需要停止使用第三方库
  • 这是ZXingObjC的官方git一步一步的,代码是从这个网站复制/粘贴的,这是一个普遍的问题,我知道SO不是调试服务,但我猜是一个帮助人们的地方,并且为了解释问题需要粘贴日志,在这种情况下,在开源 Barcodes SDK 中不存在更多在 iOS 中解码 DataMatrix 的可能性,如果我能解决这个问题,可能还有其他人在同样的问题可以解决问题,这就是SO的哲学。

标签: ios objective-c ios8 automatic-ref-counting zxing


【解决方案1】:

我认为您的项目正在使用 ARC,但 ZXING 库不是。在这种情况下,请转到 ZXING 库中的路径“目标 -> 构建阶段 -> 编译源”。双击每个 .m 文件并在弹出的对话框中输入“-fno-objc-arc”。这样,这些文件将从 ARC 中排除

【讨论】:

  • 如果您不知道如何将库源转换为 ARC,这就是您要走的路
  • @WarrenBurton:同意:)...但是由于 ZXING 是第三方库,JOANA_Batista 希望避免在不了解代码的情况下对其进行更改...我想。所以这样说。
  • 感谢您的帮助,我按照您评论的步骤执行了操作,但出现此错误:clang: error: no such file or directory: 'fno-objc-arc' Command /Applications/Xcode. app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang 失败,退出代码为 1
  • 我认为正确的标志是这里的“-fno-objc-arc”而不是“fno-objc-arc”:将“ZXingObjC”源文件夹放入您的项目中转到项目->构建阶段选择所有 ZXingObjc 文件(您可以通过按 shift 一次选择所有文件)然后按 Enter。键入“-fno-objc-arc”告诉编译器编译这些源代码时不使用arc。但无论如何我还有另一个错误:DataMatrixReader/Pods/ZXingObjC/ZXingObjC/multi/ZXByQuadrantReader.m:29:1: @synthesize of 'weak' property is allowed in ARC or GC mode
  • 抱歉打错了。更正了答案
【解决方案2】:

如果您的项目使用的是 ARC(自动引用计数),那么您不需要像为您完成的那样进行 autorelease 调用。

改变

ZXLuminanceSource *source = [[[ZXCGImageLuminanceSource alloc] initWithCGImage:imageToDecode] autorelease];

ZXLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:imageToDecode];

有关 ARC 的更多信息,请访问iOS Developer Library

【讨论】:

  • 感谢您的帮助,现在错误消失了,但是当我尝试在模拟器中执行代码时,出现了我在主要问题中显示的错误。
【解决方案3】:

这对我来说非常适合:

readme.md 中的入门指南不起作用,因此我必须执行以下步骤才能使其正常工作。

将整个文件夹复制到工作项目的子文件夹中。 并将 ZXingObjC.xcodeproj 文件拖/添加到“<your working project>”

确保在出现提示时勾选 create folder references for any added folders 选项。 然后,转到项目的 Target > Build Phases

在目标依赖项下,add ZXingObjC-iOS (ZXingObjC)

在将二进制文件与库链接下,add libZXingObjC-iOS.a

在同一类下,添加以下6个框架

AVFoundation.framework

CoreGraphics.framework

CoreMedia.framework

CoreVideo.framework

ImageIO.framework

QuartzCore.framework

来源:http://sagage.com/?p=55

我在直接设备 Iphone 4s 上的测试很好,也许不是最好的测试设备,Iphone 4s 没有“自动对焦”功能,这对于处理这种条形码非常重要。

希望这可以节省您的时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-13
    • 2013-03-11
    • 2014-09-04
    • 1970-01-01
    • 2012-08-06
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多