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 ()<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 在文中也用到了,就不贴代码了

总结

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

(0)

相关推荐

  • iOS点击查看大图的动画效果

    对于图片来说,除了表情包,几乎都会被点击查看大图.今天就讲解一个查看和收起大图的动画效果,先直接看效果图: 如图所示,最开始是一个小图,点击小图可以查看大图.大图会从小图的位置和大小"弹"出来,同时背景变成半透明的阴影.点击大图或者阴影后,收起大图,同样地弹回到小图去,同时去掉阴影背景,就像是一张图片在伸大缩小一样. 现在看看这是怎么实现的.在思考一个动画的实现方法时,把动画的动作进行分解然后再一个个去思考怎么实现是一个好的习惯,我们稍微分解一下,这个动画在显示大图和收起大图的时候做了

  • 利用iOS实现系统相册大图浏览功能详解

    前言 本文主要给大家介绍了关于iOS实现系统相册大图浏览功能的相关资料,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. 最终效果图 大图浏览 实现过程 创建两个UICollectionView分别放置大图和缩略图 实现大图和缩略图的联动 实现当前展示的大图对应的缩略图放大效果 实现原理 创建collectionView非常简单,只要正常创建就好,值得注意的是:由于需要当前展示的图片的缩略图始终保持在屏幕中间位置,所以在创建缩略图的collectionView的时候需要对coll

  • iOS从系统相册选取多张照片示例代码

    之前写过UIImagePickerController调取系统相册图片(选取单张照片,多用于用户头像) 1.pod导入 pod 'CTAssetsPickerController' 2.添加头文件 #import <CTAssetsPickerController/CTAssetsPickerController.h> 3.添加delegate <CTAssetsPickerControllerDelegate> 4.实现 - (void)upLoading { [PHPhotoL

  • iOS中获取系统相册中的图片实例

    本文介绍了iOS中获取系统相册中的图片,在很多应用中都能用到,可以获取单张图片,也可以同时获取多张图片,废话不多说了,看下面吧. 一.获取单张图片 思路: 1.利用UIImagePickerController可以从系统自带的App(照片\相机)中获得图片 2.设置代理,遵守代理协议 注意这个UIImagePickerController类比较特殊,需要遵守两个代理协议 @interface ViewController () <UIImagePickerControllerDelegate,

  • IOS获取系统相册中照片的示例代码

    先来看看效果图 下面话不多少,我们直接上代码: #import "ViewController.h" @interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate> @property (weak, nonatomic) IBOutlet UIImageView *IconView; @end @implementation ViewController

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

    照例先上效果图 本次用的第三方框架做这个,但是需要考虑的地方也比较多,怎么把拍照和相册选取结合.删除照片后添加新照片时候的相册状态等等,所有的改变都是在操作数组.还需考虑图片的压缩上传. 本次用的第三方框架为:QBImagePickerController 按照惯例,上代码,本次代码较多 第一个控制器 .h里没啥代码 #import "RRZShowEditViewController.h" #import "RRZSendShowTextCell.h" #impo

  • ios开发UITableViewCell图片加载优化详解

    目录 前言 图片自适应比例 XHWebImageAutoSize 仅加载当前屏幕的内容 预加载 前言 我们平时用UITableView用的很多,所以对列表的优化也是很关注的.很多时候,我们设置UIImageView,都是比例固定好宽高的,然后通过 scaleAspectFill 和 clipsToBounds 保持图片不变形,这样子做开发的效率是很高的,毕竟图片宽高我们都是固定好的了. 那如果产品要求图片按真正的比例展示出来呢?如果服务器有返回宽和高,那就好办了,那如果没有呢,我们应该怎么去做呢

  • iOS开发之图片模糊效果的五种实现代码

    前言 在iOS开发中我们经常会用到模糊效果使我们的界面更加美观,而iOS本身也提供了几种达到模糊效果的API,如:Core Image,使用Accelerate.Framework中的vImage API,在iOS 7之前系统的类提供UIToolbar,在iOS 8之后苹果新增加的一个类UIVisualEffectView:另外也有一些牛人写的第三方框架,如:GPUImage.本篇就针对这五种方式讲解一下具体的实现. 正文 下面就按照这五种方式,将其实现模糊效果的具体实现一一讲解一下: 在iOS

  • iOS开发中文件的上传和下载功能的基本实现

    文件的上传 说明:文件上传使用的时POST请求,通常把要上传的数据保存在请求体中.本文介绍如何不借助第三方框架实现iOS开发中得文件上传. 由于过程较为复杂,因此本文只贴出部分关键代码. 主控制器的关键代码: 复制代码 代码如下: YYViewController.m #import "YYViewController.h" #define YYEncode(str) [str dataUsingEncoding:NSUTF8StringEncoding] @interface YYV

  • iOS中tableview实现编辑、全选及删除等功能的方法示例

    前言 我们在日常开发过程中或多或少都会遇到tableview的各种功能,这里简单记录一下tableview的删除和全选删除功能,废话不多说先看一下效果图 既然拿到了需求,就应该想一下如何去实现了,对照上面图片的内容,应该如何实现呢? 看完上图之后发现用到的几个功能: 第一个:左滑删除 第二个:全选删除 左边滑动删除 实现几个代理方法后就可以了 -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationBut

  • 详解IOS开发中图片上传时两种图片压缩方式的比较

    IOS 图片上传时两种图片压缩方式的比较 上传图片不全面的想法:把图片保存到本地,然后把图片的路径上传到服务器,最后又由服务器把路径返回,这种方式不具有扩展性,如果用户换了手机,那么新手机的沙盒中就没有服务器返回的图片路径了,此时就无法获取之前已经上传了的头像了,在项目中明显的不可行. 上传图片的正确方式:上传头像到服务器一般是将图片NSData上传到服务器,服务器返回一个图片NSString地址,之后再将NSString的路径转为url并通过url请求去更新用户头像(用户头像此时更新的便是NS

  • iOS开发实现图片浏览功能

    本文实例为大家分享了iOS实现图片浏览功能的具体代码,供大家参考,具体内容如下 这是整体的效果图: 其中main.stroyboard中的控件有2个button,2个label,一个imageView.设置他们的位置大小和背景颜色和图片.让main.storyboard连接ViewController.m 下面是它的代码: #import "ViewController.h" @interface ViewController () @property (weak, nonatomic

  • 详解iOS开发中UItableview控件的数据刷新功能的实现

    实现UItableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运行界面: 点击选中行: 修改数据后自动刷新: 三.代码示例 数据模型部分: YYheros.h文件 复制代码 代码如下: // //  YYheros.h //  10-英雄展示(数据刷新) // //  Created by apple on 14-5-29. //  Copyright (c) 2014年 itc

  • IOS开发代码分享之用nstimer实现倒计时功能

    用nstimer实现倒计时功能,废话不多说,直接上代码,详细解释请参照注释 // [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];   // - (void)timerFireMethod:(NSTimer *)theTimer {     BOOL timeStart = YES;     NSCalend

  • iOS实现相册多选图片上传功能

    本文实例为大家分享了iOS实现相册多选图片上传的具体代码,供大家参考,具体内容如下 原理:获取手机里的全部照片,显示在自定义的视图里 //获取到相册的所有图片 - (void)addAllPhotos{ @WeakObj(self); _assetsLibrary=[[ALAssetsLibrary alloc]init]; [_assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAsset

随机推荐