iOS图片的多选择,删除,和本地显示

这只在第三方ZYQAssetPickerController的基础上进行多图片选择

在点击事件写入

  UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:nil

                                                        delegate:self

                                               cancelButtonTitle:@"取消"

                                          destructiveButtonTitle:nil

                                               otherButtonTitles:@"拍照", @"从手机相册选择", nil];

    [action showInView:self.view];

#park------   UIActionSheet代理方法

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    

    if (buttonIndex == 0) {

        //判断相机是否可用

        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

            [self showHint:@"相机不可用"];

            

            return;

            

        }else {

            //打开相机

            _sourceType = UIImagePickerControllerSourceTypeCamera;

            if (!_imagePicker) {

                _imagePicker = [[UIImagePickerController alloc] init];

                [_imagePicker setAllowsEditing:YES];

                [_imagePicker setDelegate:self];

            }

            [_imagePicker setSourceType:_sourceType];

            [self presentViewController:_imagePicker animated:YES completion:nil];

        }

        

    } else if (buttonIndex == 1)

    {

        //打开相册

        //_sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        //这里不用系统的,用第三方的,可以多选

        ZYQAssetPickerController *picker = [[ZYQAssetPickerController alloc] init];

        picker.maximumNumberOfSelection = 9-[_imageArr count];

        picker.assetsFilter = [ALAssetsFilter allPhotos];

        picker.showEmptyGroups=NO;

        picker.delegate=self;

        picker.selectionFilter = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {

            if ([[(ALAsset*)evaluatedObject valueForProperty:ALAssetPropertyType] isEqual:ALAssetTypeVideo]) {

                NSTimeInterval duration = [[(ALAsset*)evaluatedObject valueForProperty:ALAssetPropertyDuration] doubleValue];

                return duration >= 5;

            } else {

                return YES;

            }

        }];

        

        [self presentViewController:picker animated:YES completion:NULL];

        

    } else {

        return;

    }

    

    

}

#pragma mark - imagePickerController代理返回方法  其中对图片进行压缩处理

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    

    //因为选择的照片可能很大, 这里做一个压缩处理

    [self dismissViewControllerAnimated:YES completion:^{

        

        UIImage *selectImage = info[@"UIImagePickerControllerEditedImage"];//选取的照片

        //压缩的的大小

        CGFloat height = selectImage.size.height;

        CGFloat width = selectImage.size.width;

        

        CGFloat ratioSize = 1;

        CGSize newSize;

        NSInteger imgMaxHightSize = 800;

        NSInteger imgMaxWidthSize = 500;

        

        

        if (height > width && height > imgMaxHightSize)

        {

            ratioSize = 1 - (height - imgMaxHightSize ) / height;

        }

        else if (width > height && width > imgMaxWidthSize)

        {

            ratioSize = 1 - (width - imgMaxWidthSize) / width;

        }

        

        

        newSize = CGSizeMake(width*ratioSize, height*ratioSize);//压缩的的大小

        UIGraphicsBeginImageContextWithOptions(newSize, YES, 1);

        // 绘制改变大小的图片

        [selectImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

        // 从当前context中创建一个改变大小后的图片

        UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

        // 使当前的context出堆栈

        UIGraphicsEndImageContext();

        

        [_imageArr addObject:scaledImage];

        [self ShowFoodImage];//显示图片

        

    }];

}


#pragma mark -ZYQAssetPickerController Delegate

-(void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)assets{

    if (assets.count==0) {

        return;

    }

    

    UIImage * image=(UIImage *)[assets objectAtIndex:0];

    [addImgButton setImage:image forState:UIControlStateNormal];

    for (int i=0; icount; i++) {

        ALAsset *asset=assets[i];

        

        UIImage *tempImg=[UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];

        //压缩的的大小

        CGFloat height = tempImg.size.height;

        CGFloat width = tempImg.size.width;

        

        CGFloat ratioSize = 1;

        CGSize newSize;

        NSInteger imgMaxHightSize = 800;

        NSInteger imgMaxWidthSize = 500;

        

        

        if (height > width && height > imgMaxHightSize)

        {

            ratioSize = 1 - (height - imgMaxHightSize ) / height;

        }

        else if (width > height && width > imgMaxWidthSize)

        {

            ratioSize = 1 - (width - imgMaxWidthSize) / width;

        }

        

        

        newSize = CGSizeMake(width*ratioSize, height*ratioSize);//压缩的的大小

        UIGraphicsBeginImageContextWithOptions(newSize, YES, 1);

        // 绘制改变大小的图片

        [tempImg drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

        // 从当前context中创建一个改变大小后的图片

        UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

        // 使当前的context出堆栈

        UIGraphicsEndImageContext();


        

        [_imageArr addObject:scaledImage];

        

    }

    [self ShowFoodImage];//显示图片

}

在写完代理方法后可以看到选择图片或者拍照取消按钮不显示,少些了一个代理

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [picker dismissViewControllerAnimated:NO completion:nil];

}

下面就是显示图片了   [ self   ShowFoodImage ]; // 显示图片,在图片进行删除

- (void)ShowFoodImage{

    

   

    if ([_imageArr count]>0) {

        int row=2;

        if ([_imageArr count]==9) {

            if (([_imageArr count])%4==0){

                row=([_imageArr count]+1)/3;

            }

            else{

                row=(([_imageArr count])/4)+1 ;

            }

        }else{

            if (([_imageArr count]+1)%4==0){

                row=([_imageArr count]+1)/4;

            }

            else{

                row=(([_imageArr count]+1)/4)+1 ;

            }

        }

        

        float add_Height=(row-1)*(Image_Height+12);

        _showImageView_Height=Image_Height+16+add_Height+20;

        [_showImageView setFrame:CGRectMake(0,_sigLabel.frame.origin.y+_sigLabel.frame.size.height,__MainView_Width-(EdgeDistance*2) ,_showImageView_Height)];

        [_showStarView setFrame:CGRectMake(0,_showImageView.frame.origin.y+_showImageView.frame.size.height ,__MainView_Width , 145+10)];

        [_recommendFoodView setFrame:CGRectMake(0,_showStarView.frame.origin.y+_showStarView.frame.size.height,__MainView_Width, _recommendFoodView_Height)];

        [_bottomView setFrame:CGRectMake(0,_recommendFoodView.frame.origin.y+_recommendFoodView.frame.size.height ,__MainView_Width, 45+25+40)];

        

        float view__Height=__MainView_Height;

        if ((commentTextView.frame.size.height+_showImageView.frame.size.height+_showStarView.frame.size.height+_recommendFoodView.frame.size.height+_bottomView.frame.size.height)>__MainView_Height) {

            view__Height=commentTextView.frame.size.height+_showImageView.frame.size.height+_showStarView.frame.size.height+_recommendFoodView.frame.size.height+_bottomView.frame.size.height+20;

        }else

        {

            view__Height=__MainView_Height;

        }

        [_scrollView setContentSize:CGSizeMake(__MainView_Width, view__Height)];

    }

    

    //移除之前的数据

    for (UIView * views in _showImageView.subviews)

    {

        [views removeFromSuperview];

    }

    

    for (int i = 0 ; i < (_imageArr.count +1); i ++)

    {

        int n = i%4;

        NSInteger f = i/4;

        

        if (i==_imageArr.count)

        {

            if ([_imageArr count]<9) {

                addImgButton=[UIButton buttonWithType:0];

                [addImgButton setImage:[UIImage imageNamed:@"imageAdd"] forState:UIControlStateNormal];

                [addImgButton setFrame:CGRectMake(Distance+(Image_Width+Distance)*n, 8+(Image_Height+12)*f+20, Image_Width, Image_Height)];

                [addImgButton addTarget:self action:@selector(addImgButtonClick:) forControlEvents:UIControlEventTouchUpInside];

                [_showImageView addSubview:addImgButton];

            }

            

            

        }else{

            

            UIButton * dragBu = [UIButton buttonWithType:UIButtonTypeRoundedRect];

            [dragBu setFrame:CGRectMake(Distance+(Image_Width+Distance)*n, 8+(Image_Height+12)*f+20, Image_Width, Image_Height)];

            dragBu.tag = 100000;

            dragBu.adjustsImageWhenHighlighted = NO;

            

            [dragBu.imageView setContentMode:UIViewContentModeScaleAspectFill];

                       [dragBu setBackgroundImage:[_imageArr objectAtIndex:i] forState:UIControlStateNormal];

            

        

            UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longAction:)];

            [dragBu addGestureRecognizer:longTap];

            

            _deleteImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];

            _deleteImage.clipsToBounds  = YES;

            

            [_deleteImage.layer setCornerRadius:_deleteImage.bounds.size.width/2];

            _deleteImage.layer.masksToBounds = YES;

            _deleteImage.layer.borderWidth = 0.5;

            _deleteImage.layer.borderColor = [[UIColor whiteColor] CGColor];


            _deleteImage.image = [UIImage imageNamed:@"delete"];

            [_deleteImage setHidden:YES];

            _deleteImage.userInteractionEnabled = YES;

            _deleteImage.tag = i;

            _deleteImage.backgroundColor = [UIColor whiteColor];

            UITapGestureRecognizer *deleteTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeImage:)];

            [_deleteImage addGestureRecognizer:deleteTap];

            

            [dragBu addSubview:_deleteImage];

            [_showImageView addSubview:dragBu];

        }

    }

}


//长安图片显示删除按钮

- (void)longAction:(UILongPressGestureRecognizer *)g

{

    UIButton *btn = (UIButton *)g.view;

    for (UIView *view in btn.subviews)

    {

        [view setHidden:NO];

    }

    

}

// 图片删除重新显示

- (void)removeImage:(UITapGestureRecognizer *)deleImageView

{

    UIImageView *image =(UIImageView *)deleImageView.view;

    

    NSInteger index = image.tag;

    

    [_imageArr removeObjectAtIndex:index];

    [self ShowFoodImage];

    

    

    

}

完成

你可能感兴趣的:(图片显示问题)