在IOS中从Imageview删除图像

时间:2018-04-24 04:51:55

标签: ios objective-c

我有图像数组在滚动视图中当我按下按钮删除按钮将放在图像视图的顶部时按下十字按钮,imageview没有被删除或淡出如何解决这个问题请任何人帮我解决这个问题问题..谢谢先生

这是我的代码

   -(void)getimagefunction
    {
        BusinessUser *busUser = [BusinessUser getBusinessUser];
        NSLog(@"%@", _GalleryimageDictionary);
        _GalleryimageArry =[[NSMutableArray alloc]init];
        NSMutableArray *resultarray =[_GalleryimageDictionary valueForKey:@"images"];
        if ([resultarray count] != 0) {
            NSString*baseurl =[NSString stringWithFormat:@"%@%@",BaseUrl,@"uploads/BusinessImages/"];
            NSString *bussinesid = [NSString stringWithFormat:@"%lld",busUser.business_id];
            NSString*checkstring =[NSString stringWithFormat:@"%@%@",baseurl,bussinesid];
            for(int i=0; i<resultarray.count;i++){
                _finalString =[NSString stringWithFormat:@"%@/%@",checkstring,[resultarray objectAtIndex:i]];
                NSURL * imageURL = [NSURL URLWithString:[_finalString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation([UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]])];
                UIImage *image = [UIImage imageWithData:imageData];

                [_GalleryimageArry addObject:image];
            }

            for(int i=0; i<_GalleryimageArry.count;i++){
                _imageV = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame) * i/2*0.7, 0, 100, 100)];

                self.Crossbtn =[[UIButton alloc]initWithFrame:CGRectMake(_imageV.image.size.width/2+80,0,20,20)];
                UIImage *btnImage = [UIImage imageNamed:@"CrossImageICon"];
                [self.Crossbtn setImage:btnImage forState:UIControlStateNormal];
                _imageV.contentMode = UIViewContentModeScaleAspectFill;
              _imageV.image = [_GalleryimageArry objectAtIndex:i];
                [_imageV setUserInteractionEnabled:YES];
                _imageV.tag = i;
                self.Crossbtn.tag =i;
                [self.Crossbtn addTarget:self action:@selector(deleteImage:) forControlEvents:UIControlEventTouchUpInside];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapMethod:)];
                [_imageV addGestureRecognizer:tap];
                [self.imageV addSubview:self.Crossbtn];
                if ([_crossstringshow isEqualToString:@"1"]) {
                      self.Crossbtn.hidden =NO;
                }
              else
              {
                    self.Crossbtn.hidden =YES;
              }
                [self.imageGalleryscroll addSubview:_imageV];
                [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            }
            self.imageGalleryscroll.delegate = self;
            index=0;



            self.imageGalleryscroll.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame) * _GalleryimageArry.count/2*0.7, CGRectGetHeight(self.imageGalleryscroll.frame));

            [self.imageGalleryscroll setPagingEnabled:YES];

            [self.imageGalleryscroll setShowsHorizontalScrollIndicator:NO];
            self.imageGalleryscroll.alwaysBounceVertical = NO;

        }

        else
        {  [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            //  self.bussiness_galleryimg.hidden=NO;
            self.imageGalleryscroll.hidden =YES;

        }

    }

    -(void)deleteImage:(UIButton *)sender
    {id superv = [sender superview];
        [superv removeFromSuperview];


    [self.GalleryimageArry removeObjectAtIndex:index];
    }

感谢advaced

1 个答案:

答案 0 :(得分:0)

在按钮上单击,您需要检索按钮标记,并将其与scrollview的所有子视图进行比较,您还需要检查子视图是否为imageview,因为您具有相同的按钮和imageview标记。

    -(void)deleteImage:(UIButton *)sender{    
         for (UIView * view in self.imageGalleryscroll.subviews) {
             if (view.tag == sender.tag && [subview isKindOfClass:[UIImageView class]]) {
                  [view removeFromSuperview];
            }
        }
    }

更新了答案,因为为scrollview启用了分页,我们可以使用相同的方法从数组中获取所选对象的索引。

      -(void)deleteImage:(UIButton *)sender{    

       int index = imageGalleryscroll.contentOffset.x / imageGalleryscroll.frame.size.width;

         for (UIView * view in self.imageGalleryscroll.subviews) {
             if (view.tag == sender.tag && [view isKindOfClass:[UIImageView class]]) {
                  [view removeFromSuperview];
            }
        }
        self.GalleryimageArry removeObjectAtIndex:index];

    }

如果此解决方法适合您,请告诉我