自定义相册选中图片

#import <UIKit/UIKit.h>

#import<AssetsLibrary/AssetsLibrary.h>

#import <AVFoundation/AVFoundation.h>

#import "PhotoEditViewController.h"

#define SettingCamera 10102


@interface OnePhotoController :UIViewController

@property (nonatomic,weak) id<PhotoEditDelegate> delegate;

@property (nonatomic,assign)BOOL isScale;


@end


#import "UIImage+FixOrientation.h"

#import "OnePhotoController.h"

#import "PhotoSampleItem.h"

#import "ETPhotoUtil.h"


@interface OnePhotoController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIAlertViewDelegate>


@property (weak,nonatomic) IBOutletUINavigationItem *naviTitleItem;

@property (nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic,strong) NSMutableArray *allGroups;

@property (nonatomic,strong) ALAssetsLibrary*library ;

@property (nonatomic,assign) BOOL firstIn;


@end

@implementation OnePhotoController


- (instancetype)init{

    self = [superinit];

    if (self) {

        _firstIn =YES;

    }

    returnself;

}


- (void)viewDidLoad {

    [superviewDidLoad];

    //设置collectionView

    self.allGroups = [[ETPhotoUtilsharedInstance] allPhotos];

    self.naviTitleItem.title = [[ETPhotoUtilsharedInstance]cameraRoll];

    [selfsetCollectionViewFlowLayout];

    [self.collectionViewreloadData];

    if ([self.allGroupscount]==0) {

        [selfisAuthorizationStatus];

    }

}

- (void)viewWillAppear:(BOOL)animated{

    [superviewWillAppear:animated];

//    if (self.allGroups.count>0&&_firstIn) {

//        NSIndexPath *indexPath= [NSIndexPath indexPathForRow:(self.allGroups.count -1) inSection:0];

//        [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:NO];

//        _firstIn = NO;

//    }

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

}

#pragma mark - Main Method

- (NSMutableArray *)allGroups{

    if (_allGroups ==nil) {

        _allGroups = [NSMutableArrayarray];

    }

    return_allGroups;

}

- (void)setCollectionViewFlowLayout{

    

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayoutalloc] init];

    layout.itemSize =CGSizeMake((kScreenWidth -30)*.25,(kScreenWidth -30)*.25);

    layout.minimumLineSpacing =6;

    layout.minimumInteritemSpacing =6;

    layout.sectionInset =UIEdgeInsetsMake(10,6, 10,6);

    self.collectionView = [[UICollectionViewalloc] initWithFrame:CGRectMake(0,64, kScreenWidth, kScreenHeight-64)collectionViewLayout:layout];

    self.collectionView.backgroundColor = [UIColor clearColor];

    [self.collectionViewregisterClass:[PhotoSampleItemclass] forCellWithReuseIdentifier:@"collectionView"];

    self.collectionView.dataSource = self;

    self.collectionView.delegate =self;

    [self.viewaddSubview:self.collectionView];

}

//取消按钮

- (IBAction)cancelAction:(UIButton *)sender {

   

    [selfdismissViewControllerAnimated:YEScompletion:nil];

}

#pragma mark - UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    returnself.allGroups.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{


    PhotoSampleItem *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:@"collectionView"forIndexPath:indexPath];

    dispatch_async(dispatch_get_global_queue(0,0), ^{

        ALAsset *alaset = [self.allGroupsobjectSafetyAtIndex:indexPath.row];

        //获取到媒体的类型

        NSString *type = [alasetvalueForProperty:ALAssetPropertyType];

        //获取到相片、视频的缩略图

        UIImage *image =nil;

        if ([selfcurrentDeviceIsIpad]){

            //获取到相片、视频的缩略图

            CGImageRef cgImage = [[alasetdefaultRepresentation] fullScreenImage];

            image = [UIImageimageWithCGImage:cgImage];

        }else{

            //获取到相片、视频的缩略图

            CGImageRef cgImage = [alasetaspectRatioThumbnail];

            image = [UIImageimageWithCGImage:cgImage];

        }

        //媒体类型是相片

        if ([typeisEqualToString:ALAssetTypePhoto])

        {

            dispatch_async(dispatch_get_main_queue(), ^{

                cell.imageView.image=image;

            });

        }

    });

    return cell;

}

- (BOOL)currentDeviceIsIpad{

    NSString* strDevice = [[UIDevicecurrentDevice].modelsubstringToIndex:4];

    if ([strDeviceisEqualToString:@"iPad"]) {

        returnYES;

    } else {

        returnNO;

    }

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    ALAsset *alasset =nil;

    alasset = [self.allGroupsobjectSafetyAtIndex:indexPath.row];

    NSArray *item= [UIImagegetHeadMetaFromAlasset:alasset];

    

    PhotoEditViewController *editVC = [PhotoEditViewControllernew];

    editVC.delegate =self.delegate;

    editVC.upImgs = item;

    editVC.isScale =self.isScale;

    [self.navigationControllerpushViewController:editVCanimated:YES];

}

#pragma  mark - **********ALAssetsGroup DataSource*************

- (void)isAuthorizationStatus{

    // 获取当前应用对照片的访问授权状态

    ALAuthorizationStatus authorizationStatus = [ALAssetsLibraryauthorizationStatus];

    // 如果没有获取访问授权,或者访问授权状态已经被明确禁止,则显示提示语,引导用户开启授权

    if (authorizationStatus ==ALAuthorizationStatusRestricted || authorizationStatus ==ALAuthorizationStatusDenied) {

        // 展示提示语

        NSDictionary *mainInfoDictionary = [[NSBundlemainBundle] infoDictionary];

        NSString *appName = [mainInfoDictionaryobjectForKey:@"CFBundleName"];

        UIAlertView * alertView = [[UIAlertViewalloc] initWithTitle:nilmessage:[NSStringstringWithFormat:@"请在设备的\"设置-隐私-照片\"选项中,允许%@访问你的手机相册", appName] delegate:selfcancelButtonTitle:@"设置"otherButtonTitles:@"稍后再说",nil];

        alertView.tag =SettingCamera;

        [alertView show];

    }

}

#pragma mark - **********AlertViewDelegate***********

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if(buttonIndex==0){

        NSURL *setUrl =nil;

        

        if (alertView.tag ==SettingCamera) {

            setUrl = [NSURLURLWithString:@"prefs:root=Privacy"];

        }

        [[UIApplicationsharedApplication] openURL:setUrl];

    }

    if (buttonIndex==1) {

        if (alertView.tag ==SettingCamera) {

            

            [selfdismissViewControllerAnimated:YEScompletion:nil];

        }

    }

}

@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值