【问题标题】:Multiple images are not selecting in iphoneiphone中没有选择多个图像
【发布时间】:2013-03-01 12:06:37
【问题描述】:

我是 iPhone 编程的新手。使用下面的代码,我在缩略图中显示图像。在那里我们可以选择多个图像,也可以选择和取消选择图像。选择一些图像后,它会在下一个视图中显示所选图像。为此,我使用了另一种方法。但问题是它在缩略图中显示图像但如果我选择任何图像意味着它显示了一些异常。谁能告诉我如何解决这个问题?

- (void)viewDidLoad
{   
    self.title=@"All Images";
        mImageSelected=[[NSMutableArray alloc]init];
    self.view.backgroundColor=[UIColor whiteColor];
    mFirst=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"one.jpg"]];
    mFirst.frame=CGRectMake(10, 10, 60, 50);

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] 
                                         initWithTarget:self 
                                         action:@selector(actionHandleTapOnImageView:)];
    mFirst.userInteractionEnabled = YES;
    [mFirst addGestureRecognizer:singleTap];
    [self.view addSubview:mFirst];
    msSec=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"two.jpg"]];
    msSec.frame=CGRectMake(80, 10, 60, 50);
    UITapGestureRecognizer *secsingleTap = [[UITapGestureRecognizer alloc] 
                                         initWithTarget:self 
                                            action:@selector(actionHandleTapOnImageView:)];
    msSec.userInteractionEnabled = YES;
    [msSec addGestureRecognizer:secsingleTap];

    [self.view addSubview:msSec];
    mThird=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"three.jpg"]];
    mThird.frame=CGRectMake(150, 10, 60, 50);
    UITapGestureRecognizer *trdsingleTap = [[UITapGestureRecognizer alloc] 
                                            initWithTarget:self 
                                            action:@selector(actionHandleTapOnImageView:)];
    mThird.userInteractionEnabled = YES;
    [mThird addGestureRecognizer:trdsingleTap];

    [self.view addSubview:mThird];
    m4th=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"four.jpg"]];
    m4th.frame=CGRectMake(220, 10, 60, 50);
    UITapGestureRecognizer *frthsingleTap = [[UITapGestureRecognizer alloc] 
                                            initWithTarget:self 
                                             action:@selector(actionHandleTapOnImageView:)];

    m4th.userInteractionEnabled = YES;
    [m4th addGestureRecognizer:frthsingleTap];
    [self.view addSubview:m4th];
    m5th=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"five.jpg"]];
    m5th.frame=CGRectMake(10, 70, 60, 50);
    UITapGestureRecognizer *fifthsingleTap = [[UITapGestureRecognizer alloc] 
                                             initWithTarget:self 
                                              action:@selector(actionHandleTapOnImageView:)];

    m5th.userInteractionEnabled = YES;
    [m5th addGestureRecognizer:fifthsingleTap];
    [self.view addSubview:m5th];
    UIButton *theSshowButton=[[UIButton alloc]init];
    theSshowButton.frame=CGRectMake(100, 300, 100, 30);
    theSshowButton.layer.borderWidth=2;
    theSshowButton.layer.borderColor=[UIColor purpleColor].CGColor;
    theSshowButton.layer.cornerRadius=5;
    theSshowButton.backgroundColor=[UIColor greenColor];
    [theSshowButton setTitle:@"Show" forState:UIControlStateNormal];
    [theSshowButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [theSshowButton addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:theSshowButton];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)actionHandleTapOnImageView:(UITapGestureRecognizer *) in_ImageSenderTap
{ 
if (in_ImageSenderTap.view.tag ==0 )
  {


     mTick=[[UIImageView alloc]initWithImage:[UIImage imageNamed: @"mark.jpg"]];
     mTick.frame=CGRectMake(0, 0, in_ImageSenderTap.view.frame.size.width*0.3,  in_ImageSenderTap.view.frame.size.height*0.4);

      [mImageSelected addObject:[in_ImageSenderTap.view accessibilityIdentifier]];

        [in_ImageSenderTap.view addSubview:mTick];


      in_ImageSenderTap.view.tag=1;
    }
       else
    {
    for (UIView *subviews in in_ImageSenderTap.view.subviews)
        {


            UIImageView *theSubview=(UIImageView*)subviews;

            [theSubview removeFromSuperview];
            [mImageSelected removeObjectIdenticalTo:[in_ImageSenderTap.view accessibilityIdentifier]];

        }
        in_ImageSenderTap.view.tag=0;
    }


}

像这样显示出一些期望。

2013-03-01 12:01:21.332 ImageStuf[588:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(0x159a012 0x12a7e7e 0x154db6a 0x154da20 0x37e0 0x4b685a 0x4b599b 0x4b70df 0x4b9d2d 0x4b9cac 0x4b1a28 0x21e972 0x21ee53 0x1fcd4a 0x1ee698 0x25fcdf9 0x25fcad0 0x150fbf5 0x150f962 0x1540bb6 0x153ff44 0x153fe1b 0x25fb7e3 0x25fb668 0x1ebffc 0x1cd2 0x1c05 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

如果我在此方法中注释下面的代码 -(void)actionHandleTapOnImageView:(UITapGestureRecognizer *) in_ImageSenderTap 表示它工作正常,但它没有将图像存储在数组中。任何人都可以告诉我如何解决这个问题 谢谢 阿斯兰

[mImageSelected addObject:[in_ImageSenderTap.view accessibilityIdentifier]];

【问题讨论】:

  • NSLog 并检查 [in_ImageSenderTap.view 可访问性标识符]。这不应该是零。

标签: iphone ios


【解决方案1】:

对于多选,您可以使用PSTCollectionView。这样一个很棒的库,它也可以在 iOS 5 中作为 gridView 工作,在 iOS6 之上它作为 UICollectionView 工作。

万事如意!!!

【讨论】:

    【解决方案2】:

    首先,既然可以添加缩略图本身,为什么还要在数组中插入accessibilityIdentifier? 这将确保您不会添加导致崩溃的nil。您没有在缩略图上设置accessibilityIdentifier

    其次,为什么要创建 5 个UITapGestureRecognizer 实例?只需addGestureRecognizer:时使用同一个实例,不需要5个版本。

    【讨论】:

      【解决方案3】:

      您尝试将 nil 插入 NSArray,因此它引发了异常。您应该检查要插入的元素。

      我觉得你可以用ALAssetLibrary来选择多张图片:

      [_assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
          if (group) {
              [self.groupArray addObject:group];
          }
      } failureBlock:^(NSError *error) {
          NSLog(@"Group not found!\n");
      }];
      

      - (void)getImages
      {
          if (!self.assetsArray) {
              _assetsArray = [[NSMutableArray alloc] init];
          }
      
          if (!self.assetsLibrary) {
              _assetsLibrary = [[ALAssetsLibrary alloc] init];
          }
      
          @autoreleasepool {
              [self.assetsGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                  if (result) {
                      TTAsset *ttAsset = [[TTAsset alloc] initWithAsset:result];
                      [self.assetsArray addObject:ttAsset];
                      [ttAsset release], ttAsset = nil;
                  }
              }];
          }
      
          [self.tableView reloadData];
      }
      

      之后,就可以使用 ALAsset 的 thumbnail 属性了。

      ELCImagePickerController 是一个很好的起点。

      【讨论】:

        猜你喜欢
        • 2018-10-31
        • 1970-01-01
        • 1970-01-01
        • 2012-10-17
        • 2016-08-24
        • 1970-01-01
        相关资源
        最近更新 更多