国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

IOS多選單選相冊圖片

2019-11-14 18:16:21
字體:
來源:轉載
供稿:網友

之前做項目讓實現多選相冊的圖片,自己寫了一個demo一直保存在電腦上,今天下午發現電腦128G的容量已經快沒有了,準備清理電腦,所以把之前做的一些demo放在博客上,以后方便用。

1.首先準備3個圖片

2.定義單元格PhoCollectionViewCell

#import <UIKit/UIKit.h>typedef void(^SelectBtnClickBlock) (BOOL isSelect);@interface PhoCollectionViewCell : UICollectionViewCell@PRoperty (weak ,nonatomic)  IBOutlet  UIImageView *  imageView;@property (weak ,nonatomic)  IBOutlet  UIImageView *  selectImageView;@property (nonatomic,copy) SelectBtnClickBlock selectBtnClickBlock;- (IBAction)selectBtnClick:(id)sender;@property (weak, nonatomic) IBOutlet UIButton *selectBtn;@end
#import "PhoCollectionViewCell.h"@implementation PhoCollectionViewCell- (void)awakeFromNib {    // Initialization code    }- (IBAction)selectBtnClick:(id)sender {    UIButton *btn=(UIButton *)sender;     btn.selected=!btn.selected;    NSLog(@"%@",@"aaaa");      _selectBtnClickBlock(btn.selected);}@end

3.創建相片Model

#import <Foundation/Foundation.h>#import <AssetsLibrary/ALAssetsLibrary.h>@interface PhoModel : NSObject@property(nonatomic,strong) ALAsset *asset;@property (nonatomic,assign) BOOL isSelected;@end
#import "PhoModel.h"@implementation PhoModel@end

 

4.獲取相冊圖片顯示圖片

#import "ViewController.h"#import <AssetsLibrary/AssetsLibrary.h>#import "AppDelegate.h"#import "PhoModel.h"#import "PhoCollectionViewCell.h"#define applicationDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)static NSInteger count = 0;@interface ViewController (){    NSMutableArray *mutableAssets;}@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        //獲取相冊中的全部照片    [self getAllPictures];    [_collectionView registerNib: [UINib nibWithNibName:@"PhoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCell"];}//獲取相冊中的全部照片-(void)getAllPictures {    mutableAssets = [[NSMutableArray alloc]init];        NSMutableArray *assetURLDictionaries = [[NSMutableArray alloc] init];    NSMutableArray *assetGroups = [[NSMutableArray alloc] init];        __block NSMutableArray *tempMutableAssets = mutableAssets;    __block ViewController *tempSelf = self;    __block NSMutableArray *tempAssetGroups = assetGroups;        [ApplicationDelegate.library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop){        if (group != nil) {            count = [group numberOfAssets];            __block int groupNum = 0;            [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){                if(asset != nil) {                    ++ groupNum;                    if([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {                        [assetURLDictionaries addObject:[asset valueForProperty:ALAssetPropertyURLs]];                        NSURL *url= (NSURL*) [[asset defaultRepresentation]url];                        NSLog(@"%@,%@",[asset valueForProperty:ALAssetPropertyDate],url);                        //                        [UIImage imageWithCGImage:[[result defaultRepresentation] fullScreenImage]];//圖片//                        [UIImage imageWithCGImage:[result thumbnail]];    //縮略圖                                                PhoModel *phoModel=[[PhoModel alloc]init];                        phoModel.asset=asset;                        phoModel.isSelected=NO;                        [tempMutableAssets addObject:phoModel];                        if (tempMutableAssets.count == groupNum) {                            [tempSelf allPhotosCollected:tempMutableAssets];                        }                    }                }            }];            [tempAssetGroups addObject:group];        }    }failureBlock:^(NSError *error){        NSLog(@"There is an error");    }];}//所有asset-(void)allPhotosCollected:(NSMutableArray *)mutableAsset{    [self.collectionView reloadData];}#pragma mark -- UICollectionViewDataSource- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {    CGSize itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width-15)/4.0, ([UIScreen mainScreen].bounds.size.width-30)/4.0);    return itemSize;}//定義展示的UICollectionViewCell的個數-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{    return mutableAssets.count+1;}//每個UICollectionView展示的內容-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    static NSString * CellIdentifier = @"CollectionViewCell";    PhoCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];    if (indexPath.row==0) {        cell.imageView.image = [UIImage imageNamed:@"0.png"];        cell.selectImageView.hidden=YES;        cell.selectBtnClickBlock=^(BOOL isSelect)        {            NSLog(@"cell1 block");        };        return cell;    }           PhoModel *phoModel = mutableAssets[indexPath.row-1];        cell.imageView.image = [UIImage imageWithCGImage:[phoModel.asset thumbnail]];        if (phoModel.isSelected) {        cell.selectImageView.image=[UIImage imageNamed:@"2.png"];    }    else    {        cell.selectImageView.image=[UIImage imageNamed:@"1.png"];    }    cell.selectImageView.hidden=NO;    cell.selectBtn.selected=phoModel.isSelected;    cell.selectBtnClickBlock=^(BOOL isSelect)    {        //單選多選標記 false 單選 true 多選        BOOL issangal=false;        if (issangal) {            for (PhoModel *tmpPhotoModel in mutableAssets) {                tmpPhotoModel.isSelected = NO;            }        }        phoModel.isSelected=isSelect;        [_collectionView reloadData];    };        return cell;}- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {    NSLog(@"%ld",indexPath.row);}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

5.效果

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 炎陵县| 文昌市| 台安县| 朝阳市| 富裕县| 揭东县| 临沭县| 原平市| 桐柏县| 宁明县| 藁城市| 万全县| 邯郸市| 安岳县| 和平区| 枣阳市| 顺义区| 吴桥县| 蕲春县| 库尔勒市| 家居| 沾化县| 通化市| 永济市| 巫溪县| 霍城县| 华安县| 都昌县| 宾川县| 蒙自县| 昌宁县| 息烽县| 贵定县| 富顺县| 南昌市| 德令哈市| 兖州市| 寿光市| 泰顺县| 修武县| 巴青县|