我不能在ZBarSDK中使用sourceType作为UIImagePickerControllerSourceTypePhotoLibrary

时间:2012-05-28 09:55:32

标签: photolibrary zbar-sdk

我在http://zbar.sourceforge.net/download.html下载ZBarSDK 1.2 当我将ZBarReaderViewController的sourceType设置为UIImagePickerControllerSourceTypeCamera时,它运行良好。 但是当我将sourceType设置为UIImagePickerControllerSourceTypePhotoLibrary或UIImagePickerControllerSourceTypeSavedPhotosAlbum时,应用程序崩溃了,我得到了如下错误:

  

2012-05-28 17:23:03.476哇[4137:10703] *断言失败    - [ZBarReaderViewController setSourceType:],/ Users / spadix / zbar / hg / sdk /iphone / ZBarReaderViewController.m:650   2012-05-28 17:23:03.626哇[4137:10703] * 终止应用程序   未被捕获的异常' NSInternalInconsistencyException',原因:   '尝试为sourceType属性'

设置不受支持的值(1)

我已经谷歌这个问题但无法找到解决方案。任何人都可以告诉我如何解决这个问题?剂量版本1.2只支持UIImagePickerControllerSourceTypeCamera?我下载的源代码是不是很糟糕?

感谢。

1 个答案:

答案 0 :(得分:2)

如果要选择已保存的图像,基本上会显示标准的iOS媒体视图控制器。用户从标准iOS媒体控制器中选择图像后,您实例化ZBarImageScanner的副本以扫描图像。如果这有意义,请跳到步骤5以查看我如何使用ZBarImageScanner扫描UIImage。否则,请尝试按照以下步骤操作。希望这会有所帮助。

  1. 首先向用户显示带选项的提醒。

    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self  
                                              cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Camera   Roll", @"Photo Library", nil];
    [sheet showInView:self.view];
    
  2. 选择时,

    - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        switch (buttonIndex) {
            case 0: sourceType = UIImagePickerControllerSourceTypeCamera; break;
            case 1: sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; break;
            case 2: sourceType = UIImagePickerControllerSourceTypePhotoLibrary; break;
            default: break;
        }
        if ( buttonIndex <= 2 ) {
            [self presentBarcodeReader:sourceType];
        }
    }
    
  3. 当且仅当sourceType == UIImagePickerControllerSourceTypeCamera使用ZBar呈现图像捕获界面时。

    - (void)presentBarcodeReader:(UIImagePickerControllerSourceType)sourceType {
        if ( sourceType == UIImagePickerControllerSourceTypeCamera ) {
            ZBarReaderViewController *reader = [ZBarReaderViewController new];
            reader.readerDelegate = self;
            reader.showsZBarControls = YES;
            reader.supportedOrientationsMask = ZBarOrientationMaskAll;
    
            ZBarImageScanner *scanner = reader.scanner;
            [scanner setSymbology: ZBAR_I25
                           config: ZBAR_CFG_ENABLE
                               to: 0];
            [self presentModalViewController:reader animated:YES];
        } else {
            UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
            mediaUI.sourceType = sourceType;
            mediaUI.mediaTypes = [UIImagePickerController         availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
            mediaUI.allowsEditing = NO;
            mediaUI.delegate = self;
            [self presentModalViewController:mediaUI animated:YES];
        }
    }
    
  4. 我们可以通过两种方式检测使用了哪种媒体选择器

    - (void)imagePickerController:(UIImagePickerController*)reader didFinishPickingMediaWithInfo:(NSDictionary*)info {
        id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
        if ( results ) {
            ZBarSymbol *symbol = nil;
            for (symbol in results) break;
    
            [reader dismissViewControllerAnimated:YES completion:^{
                [self partLookup:symbol.data];
            }];
        } else {
            UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage] ?         [info objectForKey:UIImagePickerControllerEditedImage] : [info         objectForKey:UIImagePickerControllerOriginalImage];
            [self scanImage:image];
        }
    }
    
  5. 和多汁的部分

    - (void)scanImage:(UIImage*)image {
        ZBarImage *zImage = [[ZBarImage alloc] initWithCGImage:image.CGImage];
        ZBarImageScanner *scanner = [[ZBarImageScanner alloc] init];
        [scanner setSymbology: ZBAR_I25
                       config: ZBAR_CFG_ENABLE
                           to: 0];
        [scanner scanImage:zImage];
        ZBarSymbolSet *set = [scanner results];
    
        for (ZBarSymbol *symbol in set) {
            NSLog(@"%@", symbol.data);
            // process symbol.data however you please.
        }
    }