欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

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

时间:2021-06-21 08:43:22|栏目:iOS代码|点击:

之前写过UIImagePickerController调取系统相册图片(选取单张照片,多用于用户头像)

1.pod导入

pod 'CTAssetsPickerController'

2.添加头文件

#import <CTAssetsPickerController/CTAssetsPickerController.h>

3.添加delegate

<CTAssetsPickerControllerDelegate>

4.实现

- (void)upLoading {
  [PHPhotoLibraryrequestAuthorization:^(PHAuthorizationStatus status){
    if (status !=PHAuthorizationStatusAuthorized)return;
    dispatch_async(dispatch_get_main_queue(), ^{
      CTAssetsPickerController *picker = [[CTAssetsPickerControlleralloc] init];
      picker.delegate =self;
      // 显示选择的索引
      picker.showsSelectionIndex =YES;
      // 设置相册的类型:相机胶卷 +自定义相册
      picker.assetCollectionSubtypes =@[
                        @(PHAssetCollectionSubtypeSmartAlbumUserLibrary),
                        @(PHAssetCollectionSubtypeAlbumRegular)];
      // 不需要显示空的相册
      picker.showsEmptyAlbums =NO;
      [selfpresentViewController:pickeranimated:YEScompletion:nil];
    });
  }];
}

-(BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldSelectAsset:(PHAsset *)asset
{
  NSInteger max =9;
  if (picker.selectedAssets.count >= max) {
    UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"提示"message:[NSStringstringWithFormat:@"最多选择%zd张图片", max] preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertActionactionWithTitle:@"好的"style:UIAlertActionStyleDefaulthandler:nil]];
    [picker presentViewController:alertanimated:YEScompletion:nil];
    // 这里不能使用self来modal别的控制器,因为此时self.view不在window上
    returnNO;
  }
  returnYES;
}

-(void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets
{
  
  NSArray *array =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
  NSString *documents = [arraylastObject];
  NSString *documentPath = [documentsstringByAppendingPathComponent:@"arrayXML.xml"];
  
  NSArray *dataArray = [NSArrayarrayWithArray:assets];
  
  
  [dataArray writeToFile:documentPathatomically:YES];
  
  
  
  NSArray *resultArray = [NSArrayarrayWithContentsOfFile:documentPath];
  NSLog(@"%@", documentPath);
  
  
  // 关闭图片选择界面
  [picker dismissViewControllerAnimated:YEScompletion:nil];
  
  // 遍历选择的所有图片
  self.plCollection.photoArray = assets;
  for (NSInteger i =0; i < assets.count; i++) {
    // 基本配置
    CGFloat scale = [UIScreenmainScreen].scale;
    PHImageRequestOptions *options = [[PHImageRequestOptionsalloc] init];
    options.resizeMode  =PHImageRequestOptionsResizeModeExact;
    options.deliveryMode =PHImageRequestOptionsDeliveryModeHighQualityFormat;
    
    PHAsset *asset = assets[i];
    CGSize size =CGSizeMake(asset.pixelWidth / scale, asset.pixelHeight / scale);
//    // 获取图片
    [[PHImageManagerdefaultManager] requestImageForAsset:assettargetSize:size contentMode:PHImageContentModeDefaultoptions:options resultHandler:^(UIImage *_Nullable result,NSDictionary *_Nullable info) {
      NSData *imageData =UIImageJPEGRepresentation([selfimageWithImageSimple:resultscaledToSize:CGSizeMake(200,200)], 0.5);
      [selfossUpload:imageData];

    }];
  }
}

上一篇:iOS如何封装带复制功能的UILabel示例代码

栏    目:iOS代码

下一篇:IOS面试大全之常见算法

本文标题:iOS从系统相册选取多张照片示例代码

本文地址:http://www.codeinn.net/misctech/145660.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有