热门标签 | HotTags
当前位置:  开发笔记 > IOS > 正文

IOS开发相册图片多选和删除的功能

之前小编有和大家分享过一篇关于从相册选取单张照片的文章,那么下面这篇文章跟大家分享下如何相册多图选择和删除,以及包括拍照功能,有需要的可以参考学习,下面来一起看看吧。

照例先上效果图

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

本次用的第三方框架为:QBImagePickerController

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

第一个控制器 .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 ()

@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  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  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 

第二个控制器 .h

#import 
#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 
#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()

@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&#63; num+ 1: num;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  RRZShowEditImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  if (indexPath.row 

RRZShowEditImageCell.h 就不写了,就是自定义一个UICollectionViewCell

NumberofwordsTextView 在文中也用到了,就不贴代码了

总结

以上就是从相机或相册选取多图上传的代码实现,其他的描述就不写了,希望这篇文章对大家开发IOS能有一定的帮助,如果有疑问大家可以留言交流。


推荐阅读
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 本文介绍如何通过SQL查询从JDE(JD Edwards)系统中提取所有字典数据,涵盖关键表的关联和字段选择。具体包括F0004和F0005系列表的数据提取方法。 ... [详细]
  • 如何高效创建和使用字体图标
    在Web和移动开发中,为什么选择字体图标?主要原因是其卓越的性能,可以显著减少HTTP请求并优化页面加载速度。本文详细介绍了从设计到应用的字体图标制作流程,并提供了专业建议。 ... [详细]
  • 本文详细介绍了如何通过命令行启动MySQL服务,包括打开命令提示符窗口、进入MySQL的bin目录、输入正确的连接命令以及注意事项。文中还提供了更多相关命令的资源链接。 ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 本文将详细介绍在Windows 7环境下,检查U盘启动盘是否制作成功的多种方法,包括通过BIOS设置和使用模拟启动工具。 ... [详细]
  • 本文介绍如何使用 NSTimer 实现倒计时功能,详细讲解了初始化方法、参数配置以及具体实现步骤。通过示例代码展示如何创建和管理定时器,确保在指定时间间隔内执行特定任务。 ... [详细]
  • 本文介绍了在Windows环境下使用pydoc工具的方法,并详细解释了如何通过命令行和浏览器查看Python内置函数的文档。此外,还提供了关于raw_input和open函数的具体用法和功能说明。 ... [详细]
  • 题目Link题目学习link1题目学习link2题目学习link3%%%受益匪浅!-----&# ... [详细]
  • 本文探讨了 C++ 中普通数组和标准库类型 vector 的初始化方法。普通数组具有固定长度,而 vector 是一种可扩展的容器,允许动态调整大小。文章详细介绍了不同初始化方式及其应用场景,并提供了代码示例以加深理解。 ... [详细]
  • 网络运维工程师负责确保企业IT基础设施的稳定运行,保障业务连续性和数据安全。他们需要具备多种技能,包括搭建和维护网络环境、监控系统性能、处理突发事件等。本文将探讨网络运维工程师的职业前景及其平均薪酬水平。 ... [详细]
  • 高效解决应用崩溃问题!友盟新版错误分析工具全面升级
    友盟推出的最新版错误分析工具,专为移动开发者设计,提供强大的Crash收集与分析功能。该工具能够实时监控App运行状态,快速发现并修复错误,显著提升应用的稳定性和用户体验。 ... [详细]
  • 本题涉及一棵由N个节点组成的树(共有N-1条边),初始时所有节点均为白色。题目要求处理两种操作:一是改变某个节点的颜色(从白变黑或从黑变白);二是查询从根节点到指定节点路径上的第一个黑色节点,若无则输出-1。 ... [详细]
  • 并发编程:深入理解设计原理与优化
    本文探讨了并发编程中的关键设计原则,特别是Java内存模型(JMM)的happens-before规则及其对多线程编程的影响。文章详细介绍了DCL双重检查锁定模式的问题及解决方案,并总结了不同处理器和内存模型之间的关系,旨在为程序员提供更深入的理解和最佳实践。 ... [详细]
author-avatar
永不言败LM
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有