iOS-相册图片多选和删除(含拍照)

项目开发告一段落,没之前那么忙了,简书接着更新吧。
之前有写过从相册选取单张照片,有网友留言问多图选择怎么做,本次就先更新这个吧

先上效果图

照片多选1.gif

本次用的第三方框架做这个,但是需要考虑的地方也比较多,怎么把拍照和相册选取结合、删除照片后添加新照片时候的相册状态等等,所有的改变都是在操作数组。还需考虑图片的压缩上传。

本次用的第三方框架为:QBImagePickerController 大家可以去github搜

按照惯例,上代码,本次代码较多

第一个控制器 .h里没啥代码

#import "RRZShowEditViewController.h"
#import "RRZSendShowTextCell.h"
#import "RRZSendShowImageCell.h"
#import "QBImagePickerController.h"
#import "ShowEditItem.h"

static NSString *sendShowTextCellID = @"SendShowTextCellID";
static NSString *sendShowImageCellID = @"SendShowImageCellID";

@interface RRZShowEditViewController ()<UITableViewDataSource, UITableViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate,  UIScrollViewDelegate,UIActionSheetDelegate,QBImagePickerControllerDelegate>

@property (strong, nonatomic) UITableView *myTableView;
/** model*/
@property (strong, nonatomic) ShowEditItem *showEditItem;
/** textView的text*/
@property (strong, nonatomic) NSString *valueStr;
/** 文本输入*/
@property (weak, nonatomic) NumberofwordsTextView *textView;

@property (strong, nonatomic) QBImagePickerController *imagePickerController;

@end

@implementation RRZShowEditViewController

-(ShowEditItem *)showEditItem{
    if (!_showEditItem) {
        _showEditItem = [[ShowEditItem alloc]init];
        _showEditItem.selectedImages = @[].mutableCopy;
        _showEditItem.selectedAssetURLs = @[].mutableCopy;
    }
    return _showEditItem;
}

-(QBImagePickerController *)imagePickerController{
    if (!_imagePickerController) {
        _imagePickerController = [[QBImagePickerController alloc] init];
        _imagePickerController.filterType = QBImagePickerControllerFilterTypePhotos;
        _imagePickerController.delegate = self;
        _imagePickerController.allowsMultipleSelection = YES;
        _imagePickerController.maximumNumberOfSelection = 9;
    }
    [_imagePickerController.selectedAssetURLs removeAllObjects];
    [_imagePickerController.selectedAssetURLs addObjectsFromArray:self.showEditItem.selectedAssetURLs];
    
    return _imagePickerController;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"晒一晒";
    [self setupTabelView];
    [self setupNavItem];
}

-(void)setupNavItem{
    
    UIBarButtonItem *rightItem = [UIBarButtonItem itemWithTitle:@"发布" highTitle:nil target:self action:@selector(sendClick) norColor:NavItemColor highColor:RGB_COLOR(200, 200, 200)];
    self.navigationItem.rightBarButtonItem = rightItem;
}

// 压缩图片
- (NSData *)resetSizeOfImageData:(UIImage *)source_image maxSize:(NSInteger)maxSize
{
    //先调整分辨率
    CGSize newSize = CGSizeMake(source_image.size.width, source_image.size.height);
    
    CGFloat tempHeight = newSize.height / 1024;
    CGFloat tempWidth = newSize.width / 1024;
    
    if (tempWidth > 1.0 && tempWidth > tempHeight) {
        newSize = CGSizeMake(source_image.size.width / tempWidth, source_image.size.height / tempWidth);
    }
    else if (tempHeight > 1.0 && tempWidth < tempHeight){
        newSize = CGSizeMake(source_image.size.width / tempHeight, source_image.size.height / tempHeight);
    }
    
    UIGraphicsBeginImageContext(newSize);
    [source_image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //调整大小
    NSData *imageData;
    if (UIImagePNGRepresentation(newImage)) {
        imageData = UIImagePNGRepresentation(newImage);
    }else{
        imageData = UIImageJPEGRepresentation(newImage, 0.5);
    }
    
    NSUInteger sizeOrigin = [imageData length];
    NSUInteger sizeOriginKB = sizeOrigin / 1024;
    if (sizeOriginKB > maxSize) {
        NSData *finallImageData = UIImageJPEGRepresentation(newImage,0.50);
        return finallImageData;
    }
    
    return imageData;
}

-(void)sendClick{
    
    [self.view endEditing:YES];
    
    [HUDController showProgressLabel:@""];
    
    NSString *textStr = self.textView.text;
    textStr = [textStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    if (textStr.length > 0 || self.showEditItem.selectedImages.count > 0) {
        DLog(@"发表晒一晒");
        
        NSMutableArray *tempImages = [[NSMutableArray alloc]init];
        for (int i = 0; i < self.showEditItem.selectedImages.count; i++) {
            UIImage *tempImage = self.showEditItem.selectedImages[i];
            
            NSData *imgData = [self resetSizeOfImageData:tempImage maxSize:150];
            tempImage = [UIImage imageWithData:imgData];

            [tempImages addObject:tempImage];
        }
        
        
        [[RRZNetworkController sharedController]sendShowInfoByRemark:self.textView.text myfiles:tempImages success:^(NSDictionary *data) {
            
            NSString *code = data[@"code"];
            if ([code isEqualToString:@"success"]) {
                [HUDController hideHUDWithText:@"发表成功"];
                
                if (_sendSuccessBlock) {
                    _sendSuccessBlock();
                }
                
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [HUDController hideHUD];
                NSString *message = data[@"message"];
                if (message.length > 30) {
                    message = @"上传失败";
                }
                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                [alertView show];
            }
            
        } failure:^(NSDictionary *error) {
            
            [HUDController hideHUDWithText:NetworkError];
        }];
    }else{
        [HUDController hideHUDWithText:@"文字或图片不能为空"];
    }

}

-(void)setupTabelView{
    //    添加myTableView
    _myTableView = ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        tableView.backgroundColor = [UIColor clearColor];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView registerClass:[RRZSendShowTextCell class] forCellReuseIdentifier:sendShowTextCellID];
        [tableView registerClass:[RRZSendShowImageCell class] forCellReuseIdentifier:sendShowImageCellID];
        [self.view addSubview:tableView];
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
        tableView;
    });
}

#pragma mark - UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        RRZSendShowTextCell *cell = [tableView dequeueReusableCellWithIdentifier:sendShowTextCellID];
        self.textView = cell.numberTextView;
//        cell.textValueChangedBlock = ^(NSString *valueStr){
//            weakSelf.valueStr = valueStr;
//        };
        return cell;
    }else{
        RRZSendShowImageCell *cell = [tableView dequeueReusableCellWithIdentifier:sendShowImageCellID];
        cell.item = self.showEditItem;
        __weak typeof(self) weakSelf = self;
        cell.addPicturesBlock = ^(){
            [weakSelf showActionForPhoto];
        };
        cell.deleteImageBlock = ^(ShowEditItem *item){
            weakSelf.showEditItem = item;
            [weakSelf.myTableView reloadData];
        };
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 200;
    }else{
        return 300;
    }
}

#pragma mark UIActionSheet M
- (void)showActionForPhoto{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选取", nil];
    [actionSheet showInView:self.view];
}

#pragma mark - UIActionSheetDelegate
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        DLog(@"拍照");
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIAlertView *alert= [[UIAlertView alloc] initWithTitle:nil message:@"该设备不支持拍照" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:NULL];
            [alert show];
        }else{
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.allowsEditing = NO;//设置可编辑
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:picker animated:YES completion:nil];//进入照相界面
        }
    }else if (buttonIndex == 1){
        DLog(@"相册");
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            UIAlertView *alert= [[UIAlertView alloc] initWithTitle:nil message:@"该设备不支持从相册选取文件" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:NULL];
            [alert show];
        }else{

            UINavigationController *navigationController = [[BaseNavigationController alloc] initWithRootViewController:self.imagePickerController];
            [self presentViewController:navigationController animated:YES completion:NULL];
        }
    }
}

#pragma mark UIImagePickerControllerDelegate
// 拍照回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *pickerImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self.showEditItem.selectedImages addObject:pickerImage];
    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
    [assetsLibrary writeImageToSavedPhotosAlbum:[pickerImage CGImage] orientation:(ALAssetOrientation)pickerImage.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error) {
        [self.showEditItem.selectedAssetURLs addObject:assetURL];
//        [self.showEditItem addASelectedAssetURL:assetURL];
        [self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
    }];
    [picker dismissViewControllerAnimated:YES completion:^{}];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark QBImagePickerControllerDelegate
//相册回调
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didSelectAssets:(NSArray *)assets{
    [self.showEditItem.selectedImages removeAllObjects];
    
    NSMutableArray *selectedAssetURLs = [NSMutableArray new];
    [imagePickerController.selectedAssetURLs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [selectedAssetURLs addObject:obj];
    }];
    self.showEditItem.selectedAssetURLs = selectedAssetURLs;
    
    for (int i = 0; i < assets.count; i++) {
        ALAsset *asset = assets[i];
        UIImage *tempImg = [UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
        [self.showEditItem.selectedImages addObject:tempImg];
    }
    @weakify(self);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        self.showEditItem.selectedAssetURLs = selectedAssetURLs;
        dispatch_async(dispatch_get_main_queue(), ^{
            @strongify(self);
            [self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
        });
    });
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - scrollView 
// 滚动结束编辑 收起键盘
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (scrollView == self.myTableView) {
        [self.view endEditing:YES];
    }
}

- (void)dealloc
{
    _myTableView.delegate = nil;
    _myTableView.dataSource = nil;
}

@end

第二个控制器 .h


#import <UIKit/UIKit.h>
#import "NumberofwordsTextView.h"

@interface RRZSendShowTextCell : UITableViewCell

@property (weak, nonatomic) NumberofwordsTextView *numberTextView;

@end

第二个控制器 .m


#define kTweetContentCell_ContentFont [UIFont systemFontOfSize:16]

#import "RRZSendShowTextCell.h"

@interface RRZSendShowTextCell()

@end

@implementation RRZSendShowTextCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        NumberofwordsTextView *numberTextView = [[NumberofwordsTextView alloc] init];
        numberTextView.frame = CGRectMake(7, 7, SCREEN_WIDTH-7*2, 180);
        numberTextView.wordsMaxNumer = 300;
        numberTextView.placeHolder = @"写点什么来晒一晒吧...";
        numberTextView.textFont = [UIFont systemFontOfSize:14];
        [self addSubview:numberTextView];
        self.numberTextView = numberTextView;
    }
    return self;
}

@end

第三个 .h

#import <UIKit/UIKit.h>
#import "ShowEditItem.h"

@interface RRZSendShowImageCell : UITableViewCell

@property (copy, nonatomic) void (^addPicturesBlock)();
@property (copy, nonatomic) void (^deleteImageBlock)(ShowEditItem *toDelete);
@property (nonatomic,strong) ShowEditItem *item;

@end

.m

#define kShowImageCCell_Width floorf((SCREEN_WIDTH - 15*2- 10*3)/4)

#import "RRZSendShowImageCell.h"
#import "RRZShowEditImageCell.h"
#import "UICustomCollectionView.h"

static NSString *cellID = @"RRZShowImageCCellID";
@interface RRZSendShowImageCell()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (strong, nonatomic) UICustomCollectionView *mediaView;
@property (strong, nonatomic) NSMutableDictionary *imageViewsDict;
/** <#注释#>*/
@property (strong, nonatomic) NSArray *imgs;
/** <#注释#>*/
@property (weak, nonatomic) UIButton *deleteBtn;

@end

@implementation RRZSendShowImageCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.height = 300;
        [self setupCollectionView];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH - 20, 1)];
        label.backgroundColor = RGB_COLOR(240, 240, 240);
        [self.contentView addSubview:label];
    }
    return self;
}

-(void)setupCollectionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(kShowImageCCell_Width, kShowImageCCell_Width);
    self.mediaView = [[UICustomCollectionView alloc]initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH - 2 * 15, 280) collectionViewLayout:layout];
    self.mediaView.scrollEnabled = NO;
    [self.mediaView setBackgroundColor:[UIColor clearColor]];
    [self.mediaView registerNib:[UINib nibWithNibName:NSStringFromClass([RRZShowEditImageCell class]) bundle:nil] forCellWithReuseIdentifier:cellID];
    self.mediaView.dataSource = self;
    self.mediaView.delegate = self;
    [self.contentView addSubview:self.mediaView];
}

-(void)setItem:(ShowEditItem *)item{
    _item = item;
    
    [self.mediaView reloadData];
}

#pragma mark - UICollectionViewDelegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSInteger num = self.item.selectedImages.count;
    return num < 9? num+ 1: num;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RRZShowEditImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    if (indexPath.row < self.item.selectedImages.count) {
        cell.img = self.item.selectedImages[indexPath.row];
    }else{
        cell.img = nil;
    }
    cell.deleteBtn.tag = indexPath.row;
    [cell.deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.deleteBtn = cell.deleteBtn;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == self.item.selectedAssetURLs.count || indexPath.row == self.item.selectedImages.count) {
        if (_addPicturesBlock) {
            _addPicturesBlock();
        }
    }
}

-(void)deleteBtnClick:(UIButton *)btn{
    NSInteger index = btn.tag;
    [self.item.selectedImages removeObjectAtIndex:index];
    [self.item.selectedAssetURLs removeObjectAtIndex:index];
    if (_deleteImageBlock) {
        _deleteImageBlock(_item);
    }
}

@end

RRZShowEditImageCell.h 就不写了,就是自定义一个UICollectionViewCell
NumberofwordsTextView 在文中也用到了,就不贴代码了 直接去gitHub上搜吧

以上就是从相机或相册选取多图上传的代码实现,其他的描述就不写了,有问题可以留言

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容