zoukankan      html  css  js  c++  java
  • iOS 选取上传图片界面

      1 //
      2 //  MainViewController.m
      3 //  AddPhoto
      4 //
      5 //  Created by ChenJungang on 14/11/20.
      6 //  Copyright (c) 2014年 ChenJungang. All rights reserved.
      7 //
      8 
      9 #import "MainViewController.h"
     10 #import "CustomFlowLayout.h"
     11 #import "PhotoCell.h"
     12 #import "ResuableView.h"
     13 #import <MobileCoreServices/MobileCoreServices.h>
     14 #import "DetailViewController.h"
     15 
     16 
     17 #define kImage              @"kImage"
     18 #define kImageData          @"kImageData"
     19 #define kImageReferenceUrl  @"kImageReferenceUrl"
     20 
     21 
     22 static NSString *itemIdentifier = @"ItemIdentifier";
     23 static NSString *footerReuseId = @"footerReuseId";
     24 
     25 @interface MainViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,ResuableViewDelegate,UIActionSheetDelegate>
     26 
     27 @property (strong, nonatomic)UICollectionView   *collectionView;
     28 @property (strong, nonatomic)CustomFlowLayout   *customFlowLayout;
     29 @property (strong, nonatomic)NSMutableArray     *imageArray;
     30 @property (strong, nonatomic)NSIndexPath        *indexPath;
     31 
     32 @end
     33 
     34 @implementation MainViewController
     35 
     36 - (instancetype)init{
     37     self = [super init];
     38     if (self) {
     39         _imageArray = [NSMutableArray array];
     40     }
     41     return self;
     42 }
     43 
     44 - (void)viewDidLoad {
     45     [super viewDidLoad];
     46     // Do any additional setup after loading the view from its nib.
     47     self.title = @"add photo";
     48     
     49     _customFlowLayout = [CustomFlowLayout new];
     50     _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 64, [[UIScreen mainScreen] bounds].size.width - 20, 60)
     51                                          collectionViewLayout:_customFlowLayout];
     52     _collectionView.backgroundColor = [UIColor whiteColor];
     53     
     54     if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
     55         self.edgesForExtendedLayout = UIRectEdgeNone;
     56     }
     57     
     58     [self.collectionView registerClass:[PhotoCell class]
     59             forCellWithReuseIdentifier:itemIdentifier];
     60 
     61     [self.collectionView registerClass:[ResuableView class]
     62             forSupplementaryViewOfKind:UICollectionElementKindSectionFooter
     63                    withReuseIdentifier:footerReuseId];
     64     
     65     _collectionView.dataSource = self;
     66     _collectionView.delegate = self;
     67     
     68     [self.view addSubview:_collectionView];
     69 }
     70 
     71 #pragma mark - UICollectionViewDataSource
     72 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
     73 {
     74     return [_imageArray count];
     75 }
     76 
     77 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
     78 {
     79     PhotoCell *cell = 
     80     [collectionView dequeueReusableCellWithReuseIdentifier:itemIdentifier
     81                                               forIndexPath:indexPath];
     82     
     83     if ([_imageArray count] != 0) {
     84         cell.showImageView.image = _imageArray[indexPath.row][kImage];
     85     }
     86     
     87     return cell;
     88 }
     89 
     90 #pragma mark - UICollectionViewDelegate
     91 
     92 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
     93     NSLog(@"row : %d",indexPath.row);
     94     
     95     
     96     _indexPath = indexPath;
     97     UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"操作" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" otherButtonTitles:@"查看", nil];
     98     [actionSheet showInView:self.view];
     99     
    100 }
    101 
    102 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    103     
    104     ResuableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
    105                                                                   withReuseIdentifier:footerReuseId
    106                                                                          forIndexPath:indexPath];
    107     _indexPath = indexPath;
    108     footerView.delegate = self;
    109 
    110     return footerView;
    111 }
    112 
    113 #pragma mark - UIActionSheetDelegate
    114 
    115 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    116     
    117     switch (buttonIndex) {
    118         case 0:{
    119             if ([_imageArray count] != 0) {
    120                 [_imageArray removeObjectAtIndex:_indexPath.row];
    121                 [self.collectionView reloadData];
    122             }
    123             break;
    124         }
    125         case 1:{
    126             DetailViewController *detailVC = [[DetailViewController alloc] init];
    127             detailVC.image = _imageArray[_indexPath.row][kImage];
    128             [self presentViewController:detailVC animated:YES completion:nil];
    129             break;
    130         }
    131         default:
    132             break;
    133     }
    134 }
    135 
    136 
    137 #pragma mark - UIImagePickerControllerDelegate
    138 
    139 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    140     if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeImage]) {
    141         
    142         if ([_imageArray count] != 0) {
    143             for (NSDictionary *dic in _imageArray) {
    144                 if ([[info objectForKey:UIImagePickerControllerReferenceURL] isEqual:[dic objectForKey:kImageReferenceUrl]]) {
    145                     return [self dismissViewControllerAnimated:YES completion:^{
    146                         [MBHUDHelper showWarningWithText:@"该图片已经选择"];
    147                     }];
    148                 }
    149             }
    150         }
    151         //取出选中图片的信息
    152         UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    153         NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
    154         NSString *referenceUrl = [info objectForKey:UIImagePickerControllerReferenceURL];
    155         
    156         NSDictionary *imageInfoDic = @{kImage:image, kImageData:imageData, kImageReferenceUrl:referenceUrl};
    157         
    158         [_imageArray addObject:imageInfoDic];
    159         [self.collectionView reloadData];
    160         
    161         //滚动到最右边
    162         if ([_imageArray count] != 0) {
    163                     [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:[_imageArray indexOfObject:[_imageArray lastObject]] inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
    164         }
    165     }
    166     [picker dismissViewControllerAnimated:YES completion:nil];
    167 }
    168 
    169 
    170 #pragma mark - ResuableViewDelegate
    171 
    172 - (void)clickAddPhotoBtnAction{
    173     [self getPhotoFromPhotoLibrary:UIImagePickerControllerSourceTypePhotoLibrary];
    174 }
    175 
    176 -(void)getPhotoFromPhotoLibrary:(UIImagePickerControllerSourceType)sourceType
    177 {
    178     NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
    179     if([UIImagePickerController isSourceTypeAvailable:sourceType] && mediaTypes.count > 0)
    180     {
    181         NSArray *mediaTypes=[UIImagePickerController availableMediaTypesForSourceType:sourceType];
    182         UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    183         picker.mediaTypes = mediaTypes;
    184         picker.delegate = self;
    185         picker.allowsEditing = YES;
    186         picker.sourceType = sourceType;
    187         NSString *requiredMediaType = (NSString *)kUTTypeImage;
    188         NSArray *arrMediaTypes = [NSArray arrayWithObject:requiredMediaType];
    189         [picker setMediaTypes:arrMediaTypes];
    190         [self presentViewController:picker animated:YES completion:nil];
    191     }else{
    192         UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"错误信息!" message:@"当前设备不支持拍摄功能" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
    193         [alert show];
    194     }
    195 }
    196 
    197 - (void)didReceiveMemoryWarning {
    198     [super didReceiveMemoryWarning];
    199     // Dispose of any resources that can be recreated.
    200 }
    201 
    202 
    203 @end
    MainViewController.m
     1 //
     2 //  CustomFlowLayout.m
     3 //  AddPhoto
     4 //
     5 //  Created by ChenJungang on 14/11/20.
     6 //  Copyright (c) 2014年 ChenJungang. All rights reserved.
     7 //
     8 
     9 #import "CustomFlowLayout.h"
    10 
    11 @implementation CustomFlowLayout
    12 - (instancetype)init
    13 {
    14     self = [super init];
    15     if (self)
    16     {
    17         self.itemSize                = CGSizeMake(50, 50);                  // 单元格尺寸
    18         self.sectionInset            = UIEdgeInsetsMake(0, 0, 0, 0);        // 单元格边缘
    19         self.minimumInteritemSpacing = 2.0f;                                // 横排单元格最小间隔
    20         self.minimumLineSpacing      = 2.0f;                                // 单元格最小行间距
    21         self.scrollDirection = UICollectionViewScrollDirectionHorizontal;   // 滚动方向
    22         self.footerReferenceSize     = CGSizeMake(50, 50);                  // footerView 的尺寸
    23         
    24     }
    25     return self;
    26 }
    27 
    28 @end
    CustomFlowLayout.m
     1 //
     2 //  PhotoCell.m
     3 //  AddPhoto
     4 //
     5 //  Created by ChenJungang on 14/11/20.
     6 //  Copyright (c) 2014年 ChenJungang. All rights reserved.
     7 //
     8 
     9 #import "PhotoCell.h"
    10 
    11 @implementation PhotoCell
    12 
    13 - (void)awakeFromNib {
    14     // Initialization code
    15 }
    16 
    17 - (id)initWithFrame:(CGRect)frame
    18 {
    19     self = [super initWithFrame:frame];
    20     if (self)
    21     {
    22         self.backgroundColor = [UIColor whiteColor];
    23         
    24         CGRect rect = self.bounds;
    25         rect.origin.x    += 3;
    26         rect.origin.y    += 3;
    27         rect.size.height -= 6;
    28         rect.size.width  -= 6;
    29         
    30         _showImageView = [[UIImageView alloc] initWithFrame:rect];
    31         _showImageView.contentMode = UIViewContentModeScaleAspectFill;
    32         
    33         _showImageView.layer.masksToBounds = YES;
    34         [self addSubview:_showImageView];
    35     }
    36     
    37     return self;
    38 }
    39 
    40 @end
    PhotoCell.m
     1 //
     2 //  ResuableView.h
     3 //  AddPhoto
     4 //
     5 //  Created by ChenJungang on 14/11/20.
     6 //  Copyright (c) 2014年 ChenJungang. All rights reserved.
     7 //
     8 
     9 #import <UIKit/UIKit.h>
    10 
    11 @protocol ResuableViewDelegate <NSObject>
    12 
    13 - (void)clickAddPhotoBtnAction;
    14 
    15 @end
    16 
    17 @interface ResuableView : UICollectionReusableView
    18 
    19 @property (nonatomic, strong) id<ResuableViewDelegate>delegate;
    20 
    21 @end
    ResuableView.h
     1 //
     2 //  ResuableView.m
     3 //  AddPhoto
     4 //
     5 //  Created by ChenJungang on 14/11/20.
     6 //  Copyright (c) 2014年 ChenJungang. All rights reserved.
     7 //
     8 
     9 #import "ResuableView.h"
    10 
    11 @interface ResuableView()
    12 @property (nonatomic, strong) UIButton  *addPhotoBtn;
    13 @end
    14 
    15 @implementation ResuableView
    16 
    17 -(instancetype)initWithFrame:(CGRect)frame{
    18     self = [super initWithFrame:frame];
    19     if (self) {
    20         
    21         self.backgroundColor = [UIColor whiteColor];
    22         
    23         CGRect rect = self.bounds;
    24         rect.origin.x    += 3;
    25         rect.origin.y    += 6;
    26         rect.size.height -= 12;
    27         rect.size.width  -= 6;
    28         
    29         _addPhotoBtn = [[UIButton alloc] initWithFrame:rect];
    30         _addPhotoBtn.contentMode = UIViewContentModeScaleAspectFill;
    31         [_addPhotoBtn setImage:[UIImage imageNamed:@"addImage.png"] forState:UIControlStateNormal];
    32         _addPhotoBtn.layer.masksToBounds = YES;
    33         [self addSubview:_addPhotoBtn];
    34         
    35         [_addPhotoBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    36         
    37     }
    38     return self;
    39 }
    40 
    41 - (void)buttonAction:(id)sender{
    42     if ([_delegate respondsToSelector:@selector(clickAddPhotoBtnAction)]) {
    43         [_delegate clickAddPhotoBtnAction];
    44     }
    45 }
    46 
    47 @end
    ResuableView.m
  • 相关阅读:
    Java实现 LeetCode 242 有效的字母异位词
    Java实现 LeetCode 212 单词搜索 II
    Java实现 LeetCode 212 单词搜索 II
    Java实现 LeetCode 212 单词搜索 II
    Java实现 LeetCode 212 单词搜索 II
    Java实现 LeetCode 344 反转字符串
    Java实现 洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk
    Java实现 洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk
    Java实现 洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk
    Java实现 洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk
  • 原文地址:https://www.cnblogs.com/ablettchen/p/4115349.html
Copyright © 2011-2022 走看看